Convert UTC Epoch to local date

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

    The Easiest Way

    If you have the unix epoch in milliseconds, in my case - 1601209912824

    1. convert it into a Date Object as so
    const dateObject = new Date(milliseconds)
    const humanDateFormat = dateObject.toString() 
    

    output -

    Sun Sep 27 2020 18:01:52 GMT+0530 (India Standard Time)
    
    1. if you want the date in UTC -
    const dateObject = new Date(milliseconds)
    const humanDateFormat = dateObject.toUTCString() 
    
    1. Now you can format it as you please.

提交回复
热议问题