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)
Since Node. js is a cross-platform, open-source JavaScript runtime environment, you can use the Date object methods getDate() , getMonth() , and getFullYear() to get the current day, month, and year in a Node. js application.
The current date can be formatted by using the Nodejs modules like Date Object or libraries like moment. js, dayjs. The JavaScript Date Object can be used in the program by using the following command. const date = new Date();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With