Date format in Node

18,151

Solution 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.

Solution 2

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.

Solution 3

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

Share:
18,151

Related videos on Youtube

Mayur
Author by

Mayur

The guy who knows NOTHING, And nothing IS IMPOSSIBLE. In a field that is rapidly upgrading itself. => Love with NodeJs. => PostgreSQL gives me the life lessons. (Relationship, Join, Cascade) => Frontend is my cup of tee, but the backend is Love => A bit data-scientist. You can reach me out here too, LinkedIn GitHub twitter Facebook Instagram

Updated on June 04, 2022

Comments

  • Mayur
    Mayur almost 2 years

    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
    Mayur over 5 years
    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.
  • Martin Adámek
    Martin Adámek over 5 years
    Then you will need to do the formatting yourself via Date object getters.