Get Unix Timestamp from a Date string in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 26, 2022

banner

Photo from Unsplash

Get Unix Timestamp from a Date string in JavaScript #

To get a Unix timestamp from a date string:

  1. Pass the date string to the Date() constructor.
  2. Call the getTime() method on the Date object to get a timestamp in milliseconds.
  3. Divide the result by 1000 to get a Unix timestamp.
index.js
const dateStr = '2022-04-27'; const date = new Date(dateStr); // 👇️ timestamp in milliseconds const timestampInMs = date.getTime(); // 👇️ timestamp in seconds (Unix timestamp) const timestampInSeconds = Math.floor(date.getTime() / 1000); console.log(timestampInSeconds);

We passed the date string to the Date() constructor to get a Date object.

If you get an invalid Date when creating the Date object, you need to format the string correctly before passing it to the Date() constructor (more on that below).

The getTime method returns the number of milliseconds since the Unix Epoch (the 1st of January, 1970 00:00:00).

If you need to convert the result to seconds, divide it by 1000.

In short, to convert a date string to a Unix timestamp, all you have to do is call the getTime() method on the Date and divide the result by 1000.

If you have difficulties creating a valid Date object, you can pass 2 types of parameters to the Date() constructor:

  1. a valid ISO 8601 string, formatted as YYYY-MM-DDTHH:mm:ss.sssZ, or just YYYY-MM-DD, if you only have a date without time.
  2. multiple, comma-separated parameters that represent the year, month (0 = January to 11 = December), day of the month, hours, minutes and seconds.

Here is an example where we have a date string formatted as MM/DD/YYYY hh:mm:ss (could be any other format), get each value and pass it as parameter to the Date() constructor.

index.js
// Formatted as MM/DD/YYYY hh:mm:ss const dateStr = '02/24/2022 09:26:30'; const [dateValues, timeValues] = dateStr.split(' '); console.log(dateValues); // 👉️ "02/24/2022" console.log(timeValues); // 👉️ "09:26:30" const [month, day, year] = dateValues.split('/'); const [hours, minutes, seconds] = timeValues.split(':'); const date = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); console.log(date); // 👉️ Thu Feb 24 2022 09:26:30 const timestampInMs = date.getTime(); const timestampInSeconds = Math.floor(date.getTime() / 1000);

The first thing we did was split the date and time string on the space, so we can get the date and time components as separate strings.

We then had to split the date string on each forward slash to get the values for the month, day and year. Note that your separator might be different, e.g. a hyphen, but the approach is the same.

We also split the time string on each colon and assigned the hours, minutes and seconds to variables.

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is because, the Date constructor expects a zero-based value, where January = 0, February = 1, March = 2, etc.

We passed all the values to the Date() constructor to create a Date object and got the Unix timestamp by calling the getTime() method and dividing by 1000.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.