Convert timestamp to Date time in Javascript

Advertisements

I have this timestamp 1682604000

need to convert it in this format

2023-04-27T11:23:24.000Z

but getting this date

1970-01-20T11:23:24.000Z

using this code

new Date(timestamp)).toISOString()

Any solution Thanks

>Solution :

The reason you are getting the wrong date is that the timestamp you provided (1682604000) is in seconds, not milliseconds. JavaScript’s Date constructor takes in the number of milliseconds since January 1, 1970, not seconds.

To fix this, you need to convert the timestamp from seconds to milliseconds by multiplying it by 1000.

Here’s the corrected code:

new Date(timestamp * 1000).toISOString()

Using this code with the timestamp you provided (1682604000), you will get the output you desire:
2023-04-27T11:23:20.000Z.

Leave a ReplyCancel reply