Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert timestamp to Date time in Javascript

I have this timestamp 1682604000

need to convert it in this format

2023-04-27T11:23:24.000Z

but getting this date

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading