跳到主要内容
CNode

一个简单到问题,nodejs到时间怎么格式化?

社区
Zzcl521ss发布于 13 年前最后回复 8 年前
17868320

想要2013-2-29 12:12:12 这种格式

 var nowTime = new Date();
  var daystr = nowTime.format('YYYY-MM-DD');  出错
查看回复

回复 (17)

D
dinghao1994#178 年前

为什么 这个函数 作为单独一个模块 导出之后 在主文件里 显示 不是一个函数呢

C
CoderIvan#1611 年前

moment.js

Y
yuyang041060120#1511 年前

moment.js

W
wangshikun2010#1411 年前

moment.js

D
dayuoba#1311 年前

moment+10086

P
pingchangxin#1211 年前

var nowDate = new Date(); var result = nowDate.toLocaleDateString() + " "+ nowDate.toLocaleTimeString();

I
i5ting#1112 年前
Y
youxiachai#1012 年前

moment.js

A
alsotang#912 年前

moment.js

B
bsspirit#812 年前

moment.js

S
solqkwgkwg#712 年前

发错

S
solqkwgkwg#612 年前

基本注入已完成,就差路径映射变量处理

L
leapon#513 年前

moment.js 是正解

Z
zcl521ss#213 年前
引用 hussionDate.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //...

谢谢

还有个问题,就是字符串拼接 比如:

var daystr=new Date().Format("yyyy-MM-dd HH:mm:ss"); 
 var sql="select top 10 worksite,left(NeedDeal,45) as NeedDeal,UserCoName,gz from [WorkSite] where ToDate>‘"+daystr+"’ and stop=0 order by PDate desc";

错误提示

 query: 'select top 10 worksite,left(NeedDeal,45) as NeedDeal,UserCoName,gz from [WorkSite] where ToDate>\'"2013-2-30 17:11"\' and stop=0 order by PDate desc'

变为'"2012-2-30 17:11"' 要怎么写啊?

H
hussion#113 年前
Date.prototype.Format = function (fmt) { //author: meizz 
var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "h+": this.getHours(), //小时 
    "m+": this.getMinutes(), //分 
    "s+": this.getSeconds(), //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds() //毫秒 
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;

} 调用:

var time1 = new Date().Format("yyyy-MM-dd"); var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");

参与回复

登录后即可参与回复。登录