HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Convert between a JavaScript Date object and a Unix timestamp

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptdateobjectunixandbetweentimestampconvert

Problem

Unix timestamps are a number representing the number of seconds since the Unix epoch (_January 1, 1970, 00:00:00 UTC_). JavaScript Date objects are a number representing the number of milliseconds since the Unix epoch.
This means that you can convert between Date objects and Unix timestamps by dividing or multiplying by 1000.

Solution

const toTimestamp = date => Math.floor(date.getTime() / 1000);
const fromTimestamp = timestamp => new Date(timestamp * 1000);

toTimestamp(new Date('2024-01-04')); // 1704326400
fromTimestamp(1704326400); // 2024-01-04T00:00:00.000Z

Code Snippets

const toTimestamp = date => Math.floor(date.getTime() / 1000);
const fromTimestamp = timestamp => new Date(timestamp * 1000);

toTimestamp(new Date('2024-01-04')); // 1704326400
fromTimestamp(1704326400); // 2024-01-04T00:00:00.000Z

Context

From 30-seconds-of-code: date-to-unix-timestamp

Revisions (0)

No revisions yet.