0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Timestamp to Date Easily

Use new Date(timestamp) to convert a timestamp (milliseconds since Jan 1, 1970) into a JavaScript Date object, then use methods like toLocaleString() to get a readable date.
📋

Examples

Input0
OutputThu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)
Input1672531200000
OutputSun Jan 01 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Input-86400000
OutputTue Dec 30 1969 00:00:00 GMT+0000 (Coordinated Universal Time)
🧠

How to Think About It

To convert a timestamp to a date, think of the timestamp as the number of milliseconds passed since January 1, 1970. You create a Date object using this number, and then you can display it in a human-friendly format using built-in Date methods.
📐

Algorithm

1
Get the timestamp value (milliseconds since Jan 1, 1970).
2
Create a new Date object using the timestamp.
3
Use Date methods like toLocaleString() to convert the Date object to a readable string.
4
Return or print the formatted date string.
💻

Code

javascript
const timestamp = 1672531200000;
const date = new Date(timestamp);
console.log(date.toLocaleString());
Output
1/1/2023, 12:00:00 AM
🔍

Dry Run

Let's trace the timestamp 1672531200000 through the code

1

Create Date object

new Date(1672531200000) creates a Date representing Jan 1, 2023

2

Convert to string

date.toLocaleString() returns '1/1/2023, 12:00:00 AM' (format depends on locale)

StepValue
Timestamp1672531200000
Date objectSun Jan 01 2023 00:00:00 GMT+0000
Formatted string1/1/2023, 12:00:00 AM
💡

Why This Works

Step 1: Timestamp as milliseconds

The timestamp is the number of milliseconds since January 1, 1970, which is the standard epoch time in JavaScript.

Step 2: Date object creation

Using new Date(timestamp) creates a Date object representing that exact moment in time.

Step 3: Formatting date

Calling toLocaleString() on the Date object converts it into a readable string based on your computer's locale settings.

🔄

Alternative Approaches

Using Date.UTC and manual conversion
javascript
const timestamp = 1672531200000;
const date = new Date();
date.setTime(timestamp);
console.log(date.toString());
This method sets the time of an existing Date object; it's less direct but useful if you want to reuse a Date instance.
Using Intl.DateTimeFormat for custom formatting
javascript
const timestamp = 1672531200000;
const date = new Date(timestamp);
const formatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' });
console.log(formatter.format(date));
This approach gives more control over the date format but requires more code.

Complexity: O(1) time, O(1) space

Time Complexity

Creating a Date object and formatting it takes constant time, as no loops or recursion are involved.

Space Complexity

Only a few variables are used, so space usage is constant.

Which Approach is Fastest?

Using new Date(timestamp) directly is the simplest and fastest way; alternatives add formatting flexibility but with minor overhead.

ApproachTimeSpaceBest For
new Date(timestamp)O(1)O(1)Simple conversion
Date.setTime()O(1)O(1)Reusing Date objects
Intl.DateTimeFormatO(1)O(1)Custom date formatting
💡
Remember timestamps in JavaScript are in milliseconds, not seconds.
⚠️
Using a timestamp in seconds directly without multiplying by 1000 causes wrong dates.