How do you convert Milliseconds into a Javascript UTC date?

前端 未结 3 1973
不知归路
不知归路 2021-01-06 00:09

Given I have the number 1446309338000, how do I create a JavaScript UTC date?

new Date(1446309338000) will equal a CST time (central standa

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 00:34

    It's actually as simple as homemade biscuits, If you have your date, say:

    var date_in_milliseconds = 1504640419000;
    


    You can then initialize a new date like this:

    var human_readable_date = new Date(0); //Date(0) creates a date at the Epoch, so Wed Dec 31 1969
    

    now, just add the milliseconds to the Epoch, and this will give us the desired date:

    human_readable_date.setUTCMilliseconds(date_in_milliseconds);
    

提交回复
热议问题