Last updated: Mar 6, 2024
Reading timeยท3 min
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.
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"
We used the Date() constructor to convert milliseconds to a date.
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
.
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(); // ๐๏ธ 07/25/2023 08:56:28 (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
).
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.
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
.
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.
You can learn more about the related topics by checking out the following tutorials: