Convert a Date to a Timestamp using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 20, 2022

banner

Check out my new book

Convert a Date to a Timestamp using JavaScript #

Use the getTime() method to convert a date to a timestamp, e.g. new Date().getTime(). The getTime method returns the number of milliseconds elapsed between the 1st of January, 1970 and the given date.

index.js
const str = '2022-04-26'; const date = new Date(str); // ✅ Get timestamp in Milliseconds const timestamp = date.getTime(); console.log(timestamp); // 👉️ 1650931200000 // ✅ If you need to convert to Seconds const timestampSeconds = Math.floor(date.getTime() / 1000); console.log(timestampSeconds); // 👉️ 1650931200

To convert a Date to a timestamp, you need to have a Date object.

If you have a date string, pass it 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 (1st of January, 1970 00:00:00).

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

index.js
const str = '2022-04-26'; const date = new Date(str); // ✅ If you need to convert to Seconds const timestampSeconds = Math.floor(date.getTime() / 1000); console.log(timestampSeconds); // 👉️ 1650931200

In short, to convert a Date to a timestamp, all you have to do is call the getTime() method on the Date.

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 that splits a string and passes the parameters to the Date() constructor to create a Date object.

index.js
// 👇️ Formatted as MM/DD/YYYY const str = '04/16/2022'; const [month, day, year] = str.split('/'); const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Sat Apr 16 2022 // ✅ Get timestamp const timestamp = date.getTime(); console.log(timestamp); // 👉️ 1650056400000
We have a date string that is formatted as MM/DD/YYYY, so we split the string on each forward slash to get an array of substring containing the month, day and year.

We passed the values as the first 3 parameters to the Date() constructor to create a valid Date object, so we can get a timestamp by calling the getTime() method.

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.

Here is another example, which creates a Date that also contains the hours, minutes and seconds and gets its timestamp.

index.js
// 👇️ Formatted as MM/DD/YYYY hh:mm:ss const str = '04/16/2022 06:45:12'; const [dateComponents, timeComponents] = str.split(' '); console.log(dateComponents); // 👉️ "04/16/2022" console.log(timeComponents); // 👉️ "06:45: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); // 👉️ Sat Apr 16 2022 06:45:12 // ✅ Get timestamp const timestamp = date.getTime(); console.log(timestamp); // 👉️ 1650080712000

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.

We passed all of the parameters to the Date() constructor to create a Date object and got the timestamp by calling the getTime() method.

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.