Ntlzz93 Ntlzz93 - 5 months ago 208
Node.js Question

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

I'm using node-datetime library. I want to get current datetime with format such as Year-month-day hour-minute-second

ex : 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)

Answer

See the docs for details on format:

Returns a formatted date time string.

Store the result of your call to dt.format and don't pass this to the Date constructor:

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.