Convert UTC Epoch to local date

后端 未结 16 1699
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 10:10

I have been fighting with this for a bit now. I’m trying to convert epoch to a date object. The epoch is sent to me in UTC. Whenever you pass new Date() an epoc

16条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 11:01

    I think I have a simpler solution -- set the initial date to the epoch and add UTC units. Say you have a UTC epoch var stored in seconds. How about 1234567890. To convert that to a proper date in the local time zone:

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

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

提交回复
热议问题