Convert 17-digit precision unix time (UTC) to date fromat In javascript

后端 未结 3 1565
猫巷女王i
猫巷女王i 2021-01-20 20:35

I got time token like this from 14512768065185892 from PubNub.I need to convert this time token into following format dd/mm/yy.

Any one ple

3条回答
  •  孤街浪徒
    2021-01-20 21:12

    The Date constructor can be passed a time value that is milliseconds since the epoch (1970-01-01T00:00:00Z). The value you have seems to have 4 digits too many, so just divide by 1e4 (or whatever value is appropriate):

    var timeValue = 14512768065185892;
    document.write(new Date(timeValue/1e4));

    There are plenty of questions and answers here on how to format the output as dd/mm/yy (which is a very ambiguous format), e.g.

    function formatDMYY(d) {
      function z(n){return (n<10?'0':'') + n}
      return z(d.getDate()) + '/' + z(d.getMonth() + 1) + '/' + z(d.getFullYear()%1e3);
    }
    
    document.write(formatDMYY(new Date(14512768065185892/1e4)));

提交回复
热议问题