How to Convert Timestamp to Date in JavaScript

Here are three ways to convert Timestamp to Date.

  1. Using Date Object
  2. Using Intl.DateTimeFormat Object
  3. Using Libraries

Method 1: Using Date Object

Create a new Date object, pass the timestamp as an argument, and then use the various Date methods to get the individual date components.

let timestamp = 1631618400000;

let date = new Date(timestamp);

console.log(date);

Output

2021-09-14T11:20:00.000Z

Method 2: Using the Intl.DateTimeFormat object

You can also use the Intl.DateTimeFormat() constructor that provides a way to format dates and times according to the user’s locale.

let timestamp = 1631618400000;

const date = new Date(timestamp);
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = new Intl.DateTimeFormat('en-US', options).format(date);
console.log(formattedDate);

Output

September 14, 2021

Formatting the Date

To display the date in a specific format, you can use various methods available on the Date object.

let timestamp = 1631618400000;

const date = new Date(timestamp);

let dateString = date.toDateString();
let timeString = date.toTimeString();
let fullString = date.toLocaleString();

console.log(dateString)
console.log(timeString)
console.log(fullString)

Output

Tue Sep 14 2021
16:50:00 GMT+0530 (India Standard Time)
9/14/2021, 4:50:00 PM

You can use other Date methods like getFullYear(), getMonth(), getDate(), etc., to extract specific parts of the date and format it as you need.

Method 3: Using Libraries for More Formatting Options

If you need more control over Date Formatting or want to handle different date operations easily, you might consider using libraries like Moment.js or date-fns. These libraries provide many utility functions and make date manipulation more intuitive in JavaScript.

Moment.js simplifies dealing with these differences by providing various tools to manipulate, validate, and format dates.

const moment = require('moment');

let timestamp = 1631618400000;
let date = moment(timestamp).format('MMMM Do YYYY, h:mm:ss a');

console.log(date)

Output

September 14th 2021, 4:50:00 pm

Related posts

JavaScript Date to Timestamp

JavaScript Date toTimeString()

JavaScript Date to Another Timezone