Convert a Unix Timestamp to a Date in Vanilla JavaScript
How do you convert a Unix timestamp value into a human-readable date using vanilla JavaScript?
You can convert the Unix timestamp to a date string by following these three steps:
- Convert the unix timestamp into milliseconds by multiplying it by
1000
. - Use the newly created milliseconds value to create a date object with the
new Date()
constructor method. - Use the
.toLocaleString()
function to convert the date object into human-friendly date strings.
In this article, we'll walk you through each of those steps.
Let's get started!
Table of Contents
- Convert the Unix Timestamp to Milliseconds
- Create a Date Object Using new Date()
- Create Human-Friendly Date Strings With .toLocaleString()
Convert the Unix Timestamp to Milliseconds
Since the new Date()
function needs to be supplied with a milliseconds value, we need to first convert our given Unix timestamp to milliseconds. We can do this by simply multiplying the Unix timestamp by 1000
.
Unix time is the number of seconds that have elapsed since the Unix epoch, which is the time 00:00:00 UTC
on 1 January 1970
. It's most commonly used to create a running total of seconds when interacting with computers.
Therefore, a Unix timestamp is simply the number of seconds between a specific date and the original Unix Epoch date.
Measuring time using Unix timestamps is particularly useful because it is the same for everyone around the globe at all times since they don't observe timezones. This can be very useful for dealing with dated information on both the server and client-side of applications.
So, let's write some code to convert a Unix timestamp to milliseconds:
const unixTimestamp = 1575909015
const milliseconds = unixTimestamp * 1000 // 1575909015000
Feel free to substitute your own Unix timestamp in the code above.
In the next section, we'll put to use that milliseconds value we just created.
Create a Date Object Using new Date()
Now that we have a milliseconds value, we can create a new Date()
object.
The Date
object instance we create will represent a single moment in time and will hold data on the year, month, day, hour, minute, and second for that moment in time.
Let's add on to the code we already wrote in the last section. To create the Date
object, make your code look like this:
const unixTimestamp = 1575909015
const milliseconds = 1575909015 * 1000 // 1575909015000
const dateObject = new Date(milliseconds)
We use the new Date()
constructor and pass to it the milliseconds
variable we created in the last section.
As a result, we're left with a newly created dateObject
variable that represents the Date
object instance.
We'll use this in the next section.
Create Human-Friendly Date Strings With .toLocaleString()
Now that we have a Date
object to work with, we can start creating some human-friendly date strings.
Using the .toLocaleString()
function is one really easy way to do this. The function can be called on a data object and will return a string with a language sensitive representation of the date portion of the given date object.
Here's what a simple code example looks like (adding on to the code we have written in the past sections):
const unixTimestamp = 1575909015
const milliseconds = 1575909015 * 1000 // 1575909015000
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toLocaleString() //2019-12-9 10:30:15
As you can see, we created a human-friendly date string by calling the .toLocaleString()
on the dateObject
we created in the last section.
Here are some examples of how you can use the .toLocaleString()
to return strings of specific components of the date by passing different arguments to the .toLocaleString()
function:
const unixTimestamp = 1575909015
const milliseconds = 1575909015 * 1000 // 1575909015000
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toLocaleString() //2019-12-9 10:30:15
dateObject.toLocaleString("en-US", {weekday: "long"}) // Monday
dateObject.toLocaleString("en-US", {month: "long"}) // December
dateObject.toLocaleString("en-US", {day: "numeric"}) // 9
dateObject.toLocaleString("en-US", {year: "numeric"}) // 2019
dateObject.toLocaleString("en-US", {hour: "numeric"}) // 10 AM
dateObject.toLocaleString("en-US", {minute: "numeric"}) // 30
dateObject.toLocaleString("en-US", {second: "numeric"}) // 15
dateObject.toLocaleString("en-US", {timeZoneName: "short"}) // 12/9/2019, 10:30:15 AM CST
The .toLocaleString
takes a locales string parameter that alters results based on language and geography. In the example above, we used the "en-US"
locale tag. You can learn more about other values you can use instead here.
We also passed an object with some options in it as well. If you want to learn more, there's some good information about those here.
That was the last step!