Convert Milliseconds to a Date using JavaScript


Borislav Hadzhiev

Last updated: Jul 25, 2022


Photo from Unsplash

Convert Milliseconds to a Date using JavaScript #

Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp). The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.

const timestamp = new Date().getTime(); console.log(timestamp); // 👉️ 1642664853302 const date = new Date(timestamp); console.log(date); // 👉️ Thu Jan 20 2022 09:48:00 console.log(date.toString()); // 👉️ "Thu Jan 20 2022 09:48:00" // 👇️ Format date and time using different locales console.log(date.toLocaleString('en-US')); // 👉️ "1/20/2022, 9:50:15 AM" console.log(date.toLocaleString('en-GB')); // 👉️ "20/01/2022 09:50:15" console.log(date.toLocaleString('sv')); // 👉️ "2022-01-20 09:50:15" // 👇️ Display only date console.log(date.toLocaleDateString('en-US')); // 👉️ "1/20/2022" // 👇️ Display only time console.log(date.toLocaleTimeString('en-US')); // 👉️ "9:50:15 AM"

We used the Date() constructor to convert milliseconds to a date.

The Date() constructor takes a timestamp that represents the number of milliseconds since the 1st of January, 1970 and returns a Date object.

You can then use different methods to format the date accordingly.

In the examples we used the:

You can use these built-in methods or create your own method that formats a date.

Here is an example that creates a reusable function that formats a date as MM/DD/YYYY hh:mm:ss.

Note that you can tweak this depending on your needs, e.g. to YYYY-MM-DD or YYYY-MM-DD hh:mm:ss with very minor changes.
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), date.getFullYear(), ].join('/') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } const timestamp = new Date().getTime(); // 👇️ 01/20/2022 10:07:59 (mm/dd/yyyy hh:mm:ss) console.log(formatDate(new Date(timestamp)));

We first created the padTo2Digits function, which takes care of adding a leading zero if the month, day, hours, minutes or seconds only contain a single digit (are less than 10).

This makes our date formatting consistent, as we don't want to alternate between single and double digit values depending on which month or day of the month it is.

The formatDate function takes a Date object as a parameter and formats it as MM/DD/YYYY hh:mm:ss. However, this can be any other format with just a few tweaks.

The function makes use of the following 6 Date related methods.

  • 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.

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

  • Date.getHours - returns the hour for the specified date.

  • Date.getMinutes - returns the minutes for a date.

  • Date.getSeconds - returns the seconds of a specific date.

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

The getMonth method is zero-based, so we added 1 to its return value.

We placed the return values from the date-related methods in an array, so we can join them with a forward slash / separator.

console.log(['01', '20', '2022'].join('/')); // 👉️ '01/20/2022' console.log(['09', '24', '2024'].join('/')); // 👉️ '09/24/2024'

This gets us a date formatted as mm/dd/yyyy.

Note that you could reorder the method calls in the array to get the date formatted as dd/mm/yyyy and/or change the separator to format it as yyyy-mm-dd.

We added the return values from the time-related methods into an array and joined them with a colon separator.

console.log(['08', '30', '15'].join(':')); // 👉️ '08:30:15' console.log(['04', '25', '45'].join(':')); // 👉️ '04:25:45'

The last step is to use the addition (+) operator to add a space between the date and time-related strings.

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.