Convert UTC Epoch to local date

后端 未结 16 1701
佛祖请我去吃肉
佛祖请我去吃肉 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 10:54

    The simplest solution I've found to this, is:

    var timestamp = Date.now(), // returns milliseconds since epoch time
        normalisedTime = new Date(timestamp);
    

    Notice this doesn't have the * 1000 at the end of new Date(timestamp) statement as this (for me anyway!) always seems to give out the wrong date, ie instead of giving the year 2019 it gives the year as 51015, so just bear that in mind.

提交回复
热议问题