Convert UTC Epoch to local date

Convert UTC Epoch to local date

Asked on December 10, 2018 in Javascript.
Add Comment


  • 3 Answer(s)

    Here is the simple solution,

    Take the epoch as a initial date and add UTC units. Say you have a UTC epoch var stored in seconds. How about 1234567890. In the local time zone to convert that to a proper date .

    var utcSeconds = 1234567890;
    var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
    d.setUTCSeconds(utcSeconds);
    

    Here ,

    d is now a date (in my time zone) set to Fri Feb 13 2009 18:31:30 GMT-0500 (EST)

    Answered on December 10, 2018.
    Add Comment

    The solution is very simple. look at the code

    new Date(1394104654000)
    > Thu Mar 06 2014 06:17:34 GMT-0500 (EST)
    

    Here the miliseconds are taken by newdate() function.

    Answered on December 10, 2018.
    Add Comment

    This is the actual answer, whenever we want formatting anyway we just simply use Moment.js

    moment.utc(1234567890000).local()
    >Fri Feb 13 2009 19:01:30 GMT-0430 (VET)
    
    Answered on December 10, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.