Mayur
Mayur

Reputation: 5053

Date format in Node

I just want to help with the print date like below.

Thu Sep 06 2018 18:18:26 GMT+0530

I used

console.log(new Date())

but Output of that is

2018-09-06T12:48:25.776Z

So, I don't know how to convert it.

Upvotes: 2

Views: 24661

Answers (3)

Mayur
Mayur

Reputation: 5053

The problem is solved.

new Date().toString();

This line will convert

"2019-02-24T20:11:15.213Z"  =>  "Mon Feb 25 2019 01:38:50 GMT+0530 (India Standard Time)"

Thank you guys for spent the time on my question.

Upvotes: 1

remi.moncayo
remi.moncayo

Reputation: 59

I used dateformat (npm install --save dateformat):

const dateFormat = require('dateformat');
console.log(dateFormat(new Date(), "ddd mmm dd yyyy HH:MM:ss UTC" ));

Hope it helps.

Upvotes: 5

Martin Adámek
Martin Adámek

Reputation: 18389

You could use some third party module to do that, like moment or date-fns. Or create the string manually.

For moment, see this: https://momentjs.com/docs/#/displaying/

For date-fns, see this: https://date-fns.org/v1.28.0/docs/format

For constructing the format manually, take a look at various Date object methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 2

Related Questions