Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)

like image 882
Loint Avatar asked Jul 04 '16 10:07

Loint


People also ask

How do I get the current Date and time in node JS?

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.

How do I print the current Date in node JS?

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


1 Answers

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.

like image 148
sdgluck Avatar answered Sep 21 '22 15:09

sdgluck