nodejs 里时间怎么格式话?
 发布于 12 年前  作者 surgit  14513 次预览  最后一次回复是 12 年前  来自  

存数据库的时候可以格式化成 1980-01-02 … 这样的吗?

如果取出的时候 是在get路由的时候格式化,还是在views模版里格式化呢?

9 回复
jiyinyiyong

按 JS 的 API 手动拼这个字符串或者用 moment 模块

surgit

什么时候格式化?存入的时候还是读取的时候?

leapon

2nd 用 moment 模块

存入的时候最好保持原值,取出来用的时候再格式化。

sydney

/** ±----------------------------------------

  • @desc JS版 date 函数(原php)
  • @param String format 格式
  • @param int timestamp 时间 默认当前时间
  • @return string 格式化后的时间字符串 ±---------------------------------------- / function date(format,timestamp){ var a,jsdate = (timestamp) ? new Date(timestamp1000) : new Date(); //前导0 var pad = function(n,c){ if((n=n+’’).length<c){ return new Array(++c - n.length).join(‘0’)+n; }else{ return n; } } var f = { d: function(){ return pad(f.j(),2);}, j: function(){ return jsdate.getDate();}, m: function(){ return pad(f.n(),2);}, n: function(){ return jsdate.getMonth()+1;}, Y: function(){ return jsdate.getFullYear();}, H: function(){ return pad(jsdate.getHours(),2);}, i: function(){ return pad(jsdate.getMinutes(),2);}, s: function(){ return pad(jsdate.getSeconds(),2);} }; return format.replace(/[\]?([a-zA-Z])/g,function(t,s){ if(t!=s){ ret = s; }else if(f[s]){ ret = fs; }else{ ret = s; } return ret; }); }

可以加到 app.locals({});里面//好像是app.locals

XadillaX

试试sugarjs看