How to Convert Timestamp to Date in JavaScript

Most programming languages have helpful built-in methods, properties, and functions to make developers’ lives more comfortable. For example, the Date object in Javascript is one of the most used objects that provides us methods and properties to get the Date and related to its formatting. In this example, we will see how to convert Unix timestamp to date in a human-readable format.

How to Convert Timestamp to Date in JavaScript

To convert timestamp to date in JavaScript, convert the Unix timestamp to milliseconds by multiplying with 1000 and then create a Date object by using the new Date() function and finally use the toLocaleString() function to transform the Date object into a language-sensitive representation of the Date.

Steps to convert UNIX timestamp to date in JavaScript

Step 1: Convert Unix timestamp to milliseconds

To convert timestamp to milliseconds, use the new Date() constructor and pass the timestamp to the new Date() constructor, and multiply the output by 1000 to get the milliseconds.

Let’s say we have a timestamp like this: 1575909015

const unixTimestamp = 1575909015
const milliseconds = unixTimestamp * 1000 
console.log(milliseconds)

Output

1575909015000

We get the milliseconds from the timestamp by multiplying it by 1000.

Step 2: Convert milliseconds to Date object

To convert milliseconds to Date Object in JavaScript, use the new Date() constructor. The Date Object lets us work with dates. JavaScript Date object contains the expression for the time elapsed since the 1, Jan 1970 00:00:00 UTC in milliseconds.

Let’s continue with the above example. We got the output: 1575909015000 after converting it into milliseconds. Let’s convert this into a Date object.

const unixTimestamp = 1575909015
const milliseconds = unixTimestamp * 1000 
const dateObject = new Date(milliseconds)
console.log(dateObject)

Output

2019-12-09T16:30:15.000Z

Explanation

  1. The new Date() constructor takes a millisecond as an argument. To get the millisecond as an argument, multiply the Unix timestamp by 1000.
  2. To create a Date object, use the new Date() constructor. The Date object in the given code will describe a single moment in time and represent data in the form of year, month, day, hour, minute, and second for a moment in time.
  3. In the end, we are left will the dateObject variable, which represents the data object instance. We will use it in the final step.

Step 3: Convert dateObject to human-readable date format

To convert date object to human-readable date in JavaScript, use the toLocaleString() function. The toLocaleString() is a built-in JavaScript method that returns a string with a language-sensitive representation of a date.

Let’s continue with our example; in the previous step, we got 2019-12-09T16:30:15.000Zas output. Now, let’s pass this output to the toLocaleString() function and get the final human-readable Date we need for this example.

const unixTimestamp = 1575909015
const milliseconds = unixTimestamp * 1000 
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toLocaleString() 
console.log(humanDateFormat)

Output

12/9/2019, 10:00:15 PM

Explanation

  1. We created a human-friendly date string using the toLocaleString() function on the dateObject.
  2. The toLocaleString() function can be called on a dateObject and returns the string. We finally get a Date from the timestamp in humans readable format.

That’s it for this example.

 

Leave a Comment