Convert epoch time to Date in Javascript

In this post, we will see how to convert Convert epoch time to Date Javascript.

There are multiple ways to do it. Let’s go through them.

Using Date’s setUTCSeconds()

To convert epoch time to Date in Javascript:

  • Create Date object by passing 0 in its constructor. When you pass 0 to Date’s constructor, it will set date to epoch.
  • Pass utc seconds to setUTCSeconds() method on Date object

Output:

We created new Date object and called setUTCSeconds() method on date object.

Using Date’s constructor()

To convert epoch time to Date in Javascript:

  • Multiply epoch seconds by 1000.
  • Pass it to Date’s constructor.

Output:

We multiplied epoch time with 1000 to convert it to milliseconds and passed it to Date‘s constructor.

Convert epoch to yyyy-mm-dd in Javascript

In case, you need to convert epoch time to date format yyyy-mm-dd, you can use following code:

Output:

The part before T is in required format YYYY-MM-DD, so we get split and get first part of ISO String.

That’s all about how to convert epoch time to Date in Javascript.

Was this post helpful?

Leave a Reply

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