Convert a Unix timestamp to Time using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 22, 2022

banner

Photo from Unsplash

Convert a Unix timestamp to Time using JavaScript #

To convert a Unix timestamp to time:

  1. Create a Date object from the Unix timestamp.
  2. Use the getHours(), getMinutes() and getSeconds() methods to get the time components.
  3. Optionally, format the time as hh:mm:ss.
index.js
const unixTimestamp = 1664000732; const date = new Date(unixTimestamp * 1000); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); // 👇️ Format as hh:mm:ss const time = `${padTo2Digits(hours)}:${padTo2Digits(minutes)}:${padTo2Digits( seconds, )}`; console.log(time); // 👉️ 09:25:32 function padTo2Digits(num) { return num.toString().padStart(2, '0'); }

We used the Date() constructor to create a Date object from a Unix timestamp.

The Date() constructor takes a timestamp in milliseconds, so we had to multiply the Unix timestamp by 1000.

We used the following 3 time-related methods to get the hours, minutes and seconds:

The last step is to format the time as hh:mm:ss.

The hours, minutes or seconds could contain a single-digit (if their values are less than 10) or 2 digits.

To make the output consistently show 2 digits for each value, we have to add a leading zero if any of their values are less than 10.

The padTo2Digits function only adds a leading zero if the passed in value has a length of less than 2.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(5)); // 👉️ '05' console.log(padTo2Digits(7)); // 👉️ '07' console.log(padTo2Digits(10)); // 👉️ '10'

In the scenario where the hours, minutes or seconds already have 2 digits, we leave the values as is.

If you need to render any of the date-related data, like the year, month or day of the month, you can use the following 3 methods:

  • Date.getFullYear method - returns a four-digit number representing the year that corresponds to a date.

  • Date.getMonth - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately the getMonth method is off by 1.

  • Date.getDate - returns an integer between 1 and 31 representing the day of the month for a specific date.

The getMonth method returns a zero-based month index from 0 to 11, meaning January is 0 and December is 11.

Here is a complete example which converts a Unix timestamp into a datetime string, formatted as YYYY-MM-DD hh:mm:ss.

index.js
const unixTimestamp = 1664000732; const date = new Date(unixTimestamp * 1000); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); // 👇️ Format as hh:mm:ss const time = `${padTo2Digits(hours)}:${padTo2Digits(minutes)}:${padTo2Digits( seconds, )}`; const year = date.getFullYear(); const month = padTo2Digits(date.getMonth() + 1); const day = padTo2Digits(date.getDate()); const dateTime = `${year}-${month}-${day} ${time}`; console.log(dateTime); // 👉️ 2022-09-24 09:25:32 function padTo2Digits(num) { return num.toString().padStart(2, '0'); }

You can reorder the values, change the separators or make any other changes that suite your use case.

Note that we had to add 1 to the value the getMonth method returns because it returns a zero-based month (January = 0, February = 1, etc).

We padded the values for the month and day because they could be less than 10, and we want to output consistent results.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.