Convert a Date to Seconds using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 23, 2022

banner

Photo from Unsplash

Convert a Date to Seconds using JavaScript #

To convert a date to seconds:

  1. Create a Date object using the Date() constructor.
  2. Get a timestamp in milliseconds using the geTime() method.
  3. Convert the result to seconds by dividing by 1000.
index.js
const date = new Date('2022-04-26T09:35:24'); const seconds = Math.floor(date.getTime() / 1000); console.log(seconds); // 👉️ 1650954924

We used the Date() constructor to create a Date object.

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

We can convert the milliseconds to seconds, by dividing the number by 1000.

The Math.floor function, rounds a number down if the number has a decimal, otherwise it returns the number as is.

index.js
console.log(Math.floor(4.99)); // 👉️ 4 console.log(Math.floor(4.01)); // 👉️ 4 console.log(Math.floor(4)); // 👉️ 4

This ensures that we don't get a decimal when converting the milliseconds to seconds.

Make sure to pass the result from the division to the Math.floor function, because the number might have a decimal when converted to seconds.

If you need to create a Date object from a timestamp in seconds, multiply it by 1000 and pass it to the Date() constructor.

index.js
const seconds = 1650954924; const date = new Date(seconds * 1000); // 👇️ Tue Apr 26 2022 09:35:24 console.log(date);
The Date() constructor expects a value in milliseconds, so we have to convert the seconds back to milliseconds when creating a Date object.

If you have difficulties creating a valid Date object from a date string, 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 the 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, which creates a Date, from a string that is formatted as MM/DD/YYYY hh:mm:ss.

index.js
const str = '06/26/2022 04:35:12'; const [dateComponents, timeComponents] = str.split(' '); console.log(dateComponents); // 👉️ "06/26/2022" console.log(timeComponents); // 👉️ "04:35:12" const [month, day, year] = dateComponents.split('/'); const [hours, minutes, seconds] = timeComponents.split(':'); const date = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); console.log(date); // 👉️ Sun Jun 26 2022 04:35:12 const timestampInSeconds = Math.floor(date.getTime() / 1000); console.log(timestampInSeconds); // 👉️ 1650080712

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 value 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 of the parameters to the Date() constructor to create a Date object and converted the Date to seconds.

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.