0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Date to Timestamp Easily

To convert a date to a timestamp in JavaScript, use date.getTime() or Date.parse(dateString) which returns the number of milliseconds since January 1, 1970.
📋

Examples

Inputnew Date('2024-06-01T00:00:00Z')
Output1716998400000
Inputnew Date('December 17, 1995 03:24:00')
Output819170640000
InputDate.parse('2024-06-01T00:00:00Z')
Output1716998400000
🧠

How to Think About It

To convert a date to a timestamp, think of the date as a point in time. JavaScript stores time as milliseconds from a fixed start date (January 1, 1970). You get this number by calling getTime() on a Date object or by parsing a date string with Date.parse().
📐

Algorithm

1
Create or get a Date object representing the date you want.
2
Call the <code>getTime()</code> method on the Date object to get the timestamp in milliseconds.
3
Alternatively, use <code>Date.parse()</code> with a date string to get the timestamp directly.
4
Return the timestamp value.
💻

Code

javascript
const date = new Date('2024-06-01T00:00:00Z');
const timestamp = date.getTime();
console.log(timestamp);
Output
1716998400000
🔍

Dry Run

Let's trace converting '2024-06-01T00:00:00Z' to timestamp using getTime()

1

Create Date object

date = new Date('2024-06-01T00:00:00Z') // represents June 1, 2024 at midnight UTC

2

Get timestamp

timestamp = date.getTime() // returns 1716998400000 milliseconds

3

Output timestamp

console.log(timestamp) // prints 1716998400000

StepActionValue
1Create Date object2024-06-01T00:00:00.000Z
2Call getTime()1716998400000
3Print timestamp1716998400000
💡

Why This Works

Step 1: Date object stores time

A JavaScript Date object holds a specific moment in time internally as milliseconds from January 1, 1970.

Step 2: getTime() returns milliseconds

Calling getTime() on a Date object returns this internal millisecond count, which is the timestamp.

Step 3: Date.parse() parses strings

Date.parse() converts a date string directly into the same millisecond timestamp number.

🔄

Alternative Approaches

Using Date.now() for current timestamp
javascript
const timestampNow = Date.now();
console.log(timestampNow);
This gets the current timestamp without creating a Date object, but can't convert arbitrary dates.
Using unary plus operator on Date object
javascript
const date = new Date('2024-06-01T00:00:00Z');
const timestamp = +date;
console.log(timestamp);
The unary plus converts the Date object to its timestamp number, a shorter syntax but less explicit.

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

Time Complexity

Getting a timestamp from a Date object is a direct operation with no loops, so it runs in constant time O(1).

Space Complexity

No extra memory is needed beyond the Date object and a number variable, so space complexity is O(1).

Which Approach is Fastest?

getTime() and unary plus are equally fast; Date.parse() parses strings and may be slightly slower but still O(1).

ApproachTimeSpaceBest For
date.getTime()O(1)O(1)Converting Date objects to timestamp
Date.parse()O(1)O(1)Converting date strings to timestamp
Unary plus (+date)O(1)O(1)Short syntax for Date to timestamp
Date.now()O(1)O(1)Getting current timestamp only
💡
Use date.getTime() to get the timestamp in milliseconds from any Date object.
⚠️
Forgetting that timestamps are in milliseconds, not seconds, which can cause errors when comparing or converting.