Search code examples
javascriptdatemomentjsmoment-timezone

I need correct time from `1609891200000` without using Moment


var now = moment(1609891200000, "x").format('MMM DD h:mm A');
var x = new Date(1609891200000);
console.log(now); // Prints Jan 06 12:00 AM
console.log(x.toLocaleTimeString()); // Prints 05:30:00

I don't know why I keep getting 5:30 as time. I need a way to get the correct time i.e 12:00 AM without the use of Moment.

How can I do this?


Solution

  • You should provide a code of time zone, If there is no code, then it will provide by default

    var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
    
    // toLocaleTimeString() without arguments depends on the implementation,
    // the default locale, and the default time zone
    console.log(date.toLocaleTimeString());
    // → "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
    

    Instead, you can use

    var x = new Date(1609891200000);
    x.toGMTString() // "Wed, 06 Jan 2021 00:00:00 GMT"
    x.toUTCString() // "Wed, 06 Jan 2021 00:00:00 GMT"
    x.toISOString() // "2021-01-06T00:00:00.000Z"