Convert a Date or Date String to Timestamp in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Table of Contents

  1. Convert a Date to a Timestamp using JavaScript
  2. Converting the timestamp to seconds
  3. Convert a Date string to a Timestamp in JavaScript
  4. Convert a Date and Time string to a Timestamp in JavaScript

# 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 milliseconds to seconds // divide by 1000 const unixTimestamp = Math.floor(date.getTime() / 1000); console.log(unixTimestamp); // ๐Ÿ‘‰๏ธ 1650931200

convert date to timestamp

The code for this article is available on GitHub

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).

# Converting the timestamp to seconds

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

converting the timestamp to seconds

The code for this article is available on GitHub

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

# Convert a Date string to a Timestamp in JavaScript

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

convert date string to timestamp

The code for this article is available on GitHub
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 substrings containing the month, day and year.

We have a date string formatted as MM/DD/YYYY in the example, but this can be any other format.

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.

Once you manage to create a Date object from the date string, getting a timestamp is as easy as 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.

# Convert a Date and Time string to a Timestamp in JavaScript

If you also have time-related data, e.g. hours, minutes and seconds, pass them to the Date() constructor as well.

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

convert date and time string to timestamp

The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev