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

 我来答
匿名用户
2016-03-23
展开全部
使用npm安装moment
命令- npm install moment
var moment = require("moment");
moment().format("YYYY-MM-DD HH:mm:ss") //格式串可参考官网文档<a href="http://momentjs.com/" target="_blank">xxxxx/</a>
就烦条0o
推荐于2017-10-25 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46499
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
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”);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式