Explain Codes LogoExplain Codes Logo

Convert normal date to unix timestamp

javascript
date-format
unix-timestamp
javascript-date
Anton ShumikhinbyAnton Shumikhin·Feb 3, 2025
TLDR

For an immediate solution to convert a date to a Unix timestamp with JavaScript, use this:

// Who needs calendar dates when you can have Unix time? const unixTimestamp = Math.floor(new Date('2023-01-01').getTime() / 1000); console.log(unixTimestamp); // Let's print and see the magic.

new Date() - your ticket to the JavaScript date object. getTime() - our journey from epoch in milliseconds. Use Math.floor() to bring it back to your own time — round down to get the integer. The finishing touch - divide by 1000 to set the Unix timestamp in seconds.

Know your date formats

Your date's format plays the frontman in your concert of code. JavaScript interprets ISO 8601 format, usually 'YYYY-MM-DD'. Erroneous formats lead to faulty timestamps and, let's be honest, grand disappointment.

Incorrect format warning:

// Seems fine, right? Hold your horses, cowboy! const uncertainUnix = new Date('01-01-2023').getTime() / 1000; // Tricky JavaScript, Months are in wrong place!

Correct format showcase:

// There you go, a proper date string. const definiteUnix = new Date('2023-01-01').getTime() / 1000; // Works like the first day of Spring.

Milliseconds? Shoo! We're using Math.floor() for a well-rounded integer.

Timezone matters

Is your timestamp timezone aware? The default timezone used by JavaScript is your browser's. For UTC timestamps, add a 'Z' or use Date.UTC().

Be timezone savvy:

// Just a regular Starbucks date with 'Z' const timestampWithTimezone = new Date('2023-01-01T00:00:00Z').getTime() / 1000; // Z thing did the trick

Member of UTC club:

// Here, the date is the DJ, rockin' with UTC const utcTimestamp = Date.UTC(2023, 0, 1) / 1000; // January with a 0, Python programmers can relate, right?

Using Date.parse()

Date.parse() is also your ally. It directly returns the timestamp in milliseconds.

// See the parse in action const parsedTimestamp = Date.parse('2023-01-01') / 1000; // One parse to rule them all!

Ensure your Date.parse() is clothed in a JavaScript compatible string.

Libraries to the rescue: moment.js

Third-party libraries like moment.js are like the all-you-can-eat buffet. Specify date and format, and voila—Unix!

// Ain't no mountain high enough for moment.js const momentTimestamp = moment('2023-01-01', 'YYYY-MM-DD').unix();

For handling complex date formats and operations, moment.js sits on the throne.

Spot the timestamp units

getTime() and Date.parse() deal in milliseconds, while Unix timestamps are more into seconds. Conversion is necessary, and identifying the correct format is non-negotiable.

Date object conversion

Date strings aren't the only candidates for Unix timestamps. Existing JavaScript Date objects or newly formed date components can join the party.

Current date:

// The present- the gift that keeps on giving. const nowTimestamp = Math.floor(Date.now() / 1000); // Welcome to NOW!

Date components:

// When you have all the bits and pieces const componentTimestamp = Math.floor(new Date(2023, 0, 1, 0, 0, 0, 0).getTime() / 1000); // Slowly but surely...