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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:22:27  来源:igfitidea点击:

How to get current datetime with format Y-m-d H:M:S using node-datetime library of nodejs?

node.jsdatetime

提问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'按你的问题。