2

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.

Mayur
  • 4,345
  • 3
  • 26
  • 40
  • Checkout this other question and answer [How to format a date String](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Fernando B Sep 06 '18 at 13:05

3 Answers3

5

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.

2

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

Martin Adámek
  • 16,771
  • 5
  • 45
  • 64
  • Ya, I familiar with Moment, thanks for that. But here, I don't want to extra packages. BTW, problem solved. Just add "new Date().toString()." Thanks for your answare. – Mayur Sep 06 '18 at 13:02
  • Then you will need to do the formatting yourself via `Date` object getters. – Martin Adámek Sep 06 '18 at 13:02
1

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.

Mayur
  • 4,345
  • 3
  • 26
  • 40