Why does timestamp differ?

I was given a JSON file with timestamps.

One of them is: 1545880457898

I was able to get the actual date by doing: new Date(ticket.creationTime).toLocaleString(), and it gave me: 27/12/2018, 04:14:17.

Now, I tried converting the date: 27/12/2018 by doing: new Date(Number(split[2]),Number(split[1]),Number(split[0])).getTime(), and got: 1548543600000.

Which is very different, and especially very larger. This is a huge problem since I’m comparing them to sort them by creation time.

Anyone knows why do they differ? And how can I get the equivalent of case1?

Answer

It is because Date api takes month as number starting from 0, in this case it needs to be 11 instead of 12

new Date(Number(split[2]),Number(split[1]) - 1,Number(split[0])).getTime()

But it also won’t give exact 1545880457898, because we are not providing time to Date api

Leave a Reply

Your email address will not be published. Required fields are marked *