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

3

6

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 please provide one method to convert time stamp to date format.

Thanks In Advance

Mcgowan answered 28/12, 2015 at 5:13 Comment(0)
S
11

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)));
Skippie answered 28/12, 2015 at 5:22 Comment(4)
Thank You for this great Answer!!Mcgowan
@Skippie Can you please help me? I am trying to convert timestamp 2016-02-11 03:31:18 to pubnub timestamp of 17-digit precision unix time (UTC) something like 13406746780720711 given in reference url given by pubnub pubnub.com/docs/web-javascript/api-reference#history_example_2Iolanthe
@ShashankShah—parse the string to create a Date object (e.g. my answer here), then use getTime. Note that the precision is integer milliseconds, which might be 0 (for 1970-01-01T00:00:00Z).Skippie
@Skippie I have tried following code but still 10 digit value not 17-Digit precision timestamp function parseDateTime(s) { var b = s.split(/\D/); return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]) } date = new Date(parseDateTime("2011-01-26 13:51:50") / 1000); console.log(date.getTime()); // Gives 1296030110 </script>Iolanthe
S
1

PubNub times have an extra 7 digits of precision above standard UNIX time stamps (seconds) so the first step is to divide it by 107. That gives you 1451276806 which, when you test it in a converter, you get 12/28/2015 @ 4:26am (UTC) (using the frankly bizarre(1) US date format) so it seems to be reasonable.

In terms of using Javascript to do this, you can pass the number of milliseconds to Date to have an object instantiated for you:

var dtobj = new Date(1451276806518);

keeping in mind that the millisecond value entails you dividing by 104 (lopping off the final four digits) rather than 107.

Once you have the date object, you can use standard methods to get it in the format you want, such as:

var dtobj = new Date(1451276806518);
var dtdd = ('0' + (dtobj.getDate())).slice(-2);
var dtmm = ('0' + (dtobj.getMonth() + 1)).slice(-2);
var dtyy = ('0' + (getFullYear() % 100)).slice(-2);

document.write(dtdd + "/" + dtmm + "/" + dtyy);

(1) I swear some committee must have taken the worst bits from all date formats to indicate what needed to be discarded, then some other committee accidentally took that as a recommendation. Needless to say, I'm a big fan of ISO 8601 :-)

Schmaltzy answered 28/12, 2015 at 5:24 Comment(1)
The EMCAScript Date.prototype.toString is entirely implementation dependent, so your "committee" is a bunch of developers taking a guess at what users might want. Even TC39 can't agree to fully adopt ISO 8601, rather sad.Skippie
S
1

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();
Sprat answered 28/12, 2015 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.