'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)



Solution 1:[1]

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);

[ Run the example above: https://replit.com/@SamGluck1/so-node-datetime ]

I have amended the format string from 'm/d/Y' to 'Y-m-d' as per your question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1