Convert Milliseconds to a Date using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Convert Milliseconds to a Date using JavaScript

Use the Date() constructor to convert milliseconds to a date.

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.

index.js
const timestamp = new Date().getTime(); console.log(timestamp); // ๐Ÿ‘‰๏ธ 1673598370158 const date = new Date(timestamp); console.log(date); // ๐Ÿ‘‰๏ธ Fri Jan 13 2023 10:26:10 console.log(date.toString()); // ๐Ÿ‘‰๏ธ "Fri Jan 13 2023 10:26:10" // --------------------------------------------------------- // ๐Ÿ‘‡๏ธ 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 the date console.log(date.toLocaleDateString('en-US')); // ๐Ÿ‘‰๏ธ "1/20/2022" // ๐Ÿ‘‡๏ธ Display only the time console.log(date.toLocaleTimeString('en-US')); // ๐Ÿ‘‰๏ธ "9:50:15 AM"

convert milliseconds to date

The code for this article is available on GitHub

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.

# Formatting the date yourself

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.
index.js
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(); // ๐Ÿ‘‡๏ธ 07/25/2023 08:56:28 (mm/dd/yyyy hh:mm:ss) console.log(formatDate(new Date(timestamp)));

formatting the date yourself

The code for this article is available on GitHub

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.

index.js
console.log(['01', '20', '2022'].join('/')); // ๐Ÿ‘‰๏ธ '01/20/2022' console.log(['09', '24', '2024'].join('/')); // ๐Ÿ‘‰๏ธ '09/24/2024'
The code for this article is available on GitHub

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.

index.js
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev