Convert timestamp to Date time in Javascript

I have this timestamp 1682604000

need to convert it in this format


but getting this date


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:

Leave a Reply