node.js 如何使用nodejs的节点日期时间库获取格式为Ymd H:M:S的当前日期时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38182501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current datetime with format Y-m-d H:M:S using node-datetime library of nodejs?
提问by Loint
I'm using node-datetimelibrary. I want to get current datetime with format such as Year-month-day hour-minute-second
我正在使用节点日期时间库。我想以年月日时分秒等格式获取当前日期时间
ex : 2016-07-04 17:19:11
例如:2016-07-04 17:19:11
var dateTime = require('node-datetime');
var dt = dateTime.create();
dt.format('m/d/Y H:M:S');
console.log(new Date(dt.now()));
But my result such as:
但我的结果如:
Mon Jul 04 2016 17:19:11 GMT+0700 (SE Asia Standard Time)
2016 年 7 月 4 日星期一 17:19:11 GMT+0700(东南亚标准时间)
回答by sdgluck
See the docsfor details on format:
有关以下内容的详细信息,请参阅文档format:
Returns a formatted date time string.
返回格式化的日期时间字符串。
Store the result of your call to dt.formatand don't pass this to the Dateconstructor:
存储调用的结果,dt.format不要将其传递给Date构造函数:
var dateTime = require('node-datetime');
var dt = dateTime.create();
var formatted = dt.format('Y-m-d H:M:S');
console.log(formatted);
I have amended the format string from 'm/d/Y'to 'Y-m-d'as per your question.
我从修改格式字符串'm/d/Y'来'Y-m-d'按你的问题。

