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

后端 未结 3 1563
猫巷女王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 20:52

    You can just remove the last 4 characters, and use this timestamp in Date constructor:

    new Date(+str.substr(0, str.length - 4))  
    

    However, JS doesn't support "dd/mm/yyyy" format, and you will have to implement it yourself or use third-party libraries like Moment.js.

    Here is the working demo:

    Date.parsePubNub = function(str) {
        return new Date(+str.substr(0, str.length - 4));
    };
    
    Date.prototype.toDDMMYYYY = function()
    {
      return ("0" + this.getDate()).slice(-2) + "/" + ("0" + (this.getMonth() + 1)).slice(-2) + "/" + this.getFullYear();
    };
    
    var str = "14512768065185892";
    document.body.innerText = Date.parsePubNub(str).toDDMMYYYY();

提交回复
热议问题