How to Convert Timestamp to a Date in JavaScript?

The UNIX timestamp is defined as the total number of seconds elapsed since January 1st, 1970 00:00:00 UTC.

In JavaScript, if you want to get the current timestamp, you can simply call the Date.now() method. The Date.now() method returns the value of the current timestamp in milliseconds.

But if you already have a value of the timestamp and you want to convert it to a date object, you can simply pass the timestamp to the Date() constructor as an argument.

Passing the timestamp to the Date() constructor as an argument returns a date object containing the year, month, day, hour, minutes and seconds.

For example,

// Timestamp in milliseconds
const timestamp = 1679209516375;

// Convert the timestamp to a date object
const date = new Date(timestamp);

console.log(date);
// Output: 👉 Sun Mar 19 2023 12:35:16 GMT+0530 (India Standard Time)

The UNIX timestamp is in seconds whereas the timestamp in JavaScript is in milliseconds. Therefore, when you are converting the timestamp to date using the Date() constructor, you have to make sure that it is in milliseconds.

If not, you have to multiply it by 1000 before passing it as an argument to the Date() constructor.

For example,

// Timestamp in seconds
const timestampSeconds = 1679217880;

// Timestapm in milliseconds
const timestampinMill = timestampSeconds * 1000;

// Get the date object
const date = new Date(timestampinMill)

console.log(date);
// Output: 👉 Sun Mar 19 2023 14:54:40 GMT+0530 (India Standard Time)

Convert a Given Date Back to Timestamp

If you have a date string and you want to convert it back to the timestamp, you can pass the date string to the Date() constructor as an argument to get the date object and then call the getTime() method on the date object to get the timestamp.

The getTime() method returns the timestamp in milliseconds.

For example,

// Date string
const dateStr = 'Sun Mar 19 2023 14:54:40';

// Get timestamp from date 
const timestamp = new Date(dateStr).getTime();

console.log(timestamp);
// Output: 👉 1679217880000

In place of the getTime() method, you can also call the valueOf() method.

The valueOf() method also returns the timestamp in milliseconds.

For example,

// Date string
const dateStr = 'Sun Mar 19 2023 14:54:40';

// Get timestamp from date 
const timestamp = new Date(dateStr).valueOf();

console.log(timestamp);
// Output: 👉 1679217880000

Getting the Year, Month, Day, Hours, Minutes and Seconds from the Timestamp

If you want to get the value of the year, month, day, hours, minutes, and seconds from the date object received from the timestamp, you can use the following methods of the date object:

  • getFullYear() – Returns the year for the given date
  • getMonth() – Returns the month index(0 based) for the given date
  • getDate() – Returns the day of the month for the specified date
  • getHours() – Returns hours for the specified date
  • getMinutes() – Returns minutes for the specified date
  • getSeconds() – Returns seconds for the specified date

For example,

// Timestamp in milliseconds
const timestamp = 1679209516375;

// Convert the timestamp to a date object
const date = new Date(timestamp);

console.log(date);
// Output: 👉 Sun Mar 19 2023 12:35:16 GMT+0530 (India Standard Time)

// Get different parts of the date
const year = date.getFullYear();  // Output: 👉 2023
const month = date.getMonth() + 1;  // Output: 👉 3
const day = date.getDate();  // Output: 👉  19
const hours = date.getHours();  // Output: 👉 12
const minutes = date.getMinutes();  // Output: 👉 35 
const seconds = date.getSeconds();  // Output: 👉  16

Please note that the getMonth() method returns the index(0 based) of the month for the specified date, not the month number, therefore, we add +1 to get the month number.

There are a lot of other built-in date methods available that you can use to format the date string eg. toDateString(), toLocaleDateString(), toLocaleString(), etc.

For example,

// Timestamp in milliseconds
const timestamp = 1679209516375;

// Convert the timestamp to a date object
const date = new Date(timestamp);

date.toDateString(); // Output: 👉 "Sun Mar 19 2023"
date.toLocaleDateString(); // Output: 👉 "3/19/2023"
date.toLocaleString(); // Output: 👉 "3/19/2023, 12:35:16 PM"

The toLocaleDateString() and toLocaleString() methods are almost the same except for the fact that the latter one gives you the time also.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.