Convert a Date string to a Timestamp using JavaScript

avatar

Borislav Hadzhiev

Wed Jan 19 20223 min read

banner

Photo by Alex Knight

Convert a Date string to a Timestamp using JavaScript #

To convert a date string to a timestamp:

  1. Pass the date string to the Date() constructor.
  2. Call the getTime() method on the Date object.
  3. The getTime method returns the number of milliseconds since the Unix Epoch.
index.js
// ✅ with Date formatted as YYYY-MM-DD const dateStr1 = '2022-06-24'; const date1 = new Date(dateStr1); const timestamp = date1.getTime(); console.log(timestamp); // 👉️ 1656028800000 // 👇️ If you need to convert milliseconds to seconds // divide by 1000 const inSeconds = Math.floor(date1.getTime() / 1000); // ✅ with Date in other format, e.g. MM/DD/YYYY const dateStr = '06/24/2022'; const [month, day, year] = dateStr.split('/'); const date2 = new Date(+year, month - 1, +day); console.log(date2);

In the first example, we had a date string formatted as YYYY-MM-DD, so we were able to directly pass the date string as a parameter to the Date() constructor and get a Date object back.

To get a timestamp, we called the getTime method on the Date object.

The getTime method returns a number that represents the milliseconds elapsed between the 1st of January, 1970 and the given date.

If you need to convert the timestamp to seconds instead, divide the number by 1000.

index.js
const dateStr1 = '2022-06-24'; const date1 = new Date(dateStr1); const timestamp = date1.getTime(); console.log(timestamp); // 👉️ 1656028800000 // 👇️ If you need to convert milliseconds to seconds // divide by 1000 const inSeconds = Math.floor(date1.getTime() / 1000);

If your date string cannot be passed directly to the Date() constructor, you can split it on the separator and pass the date components as parameters to the Date() constructor.

index.js
// ✅ with Date in other format, e.g. MM/DD/YYYY const dateStr = '06/24/2022'; const [month, day, year] = dateStr.split('/'); const date2 = new Date(+year, month - 1, +day); console.log(date2); // 👉️ Fri Jun 24 2022 // 👇️ Get timestamp const timestamp = date2.getTime();

The Date() constructor is most commonly called with a string formatted as ISO 8601 - YYYY-MM-DDTHH:mm:ss.sssZ or with parameters for the year, month (0 = January, 1 = February, etc.), day of the month, hours, minutes and seconds.

In the example above, we had a date string formatted as mm/dd/yyyy, but this can be any other format.

We split the string on each forward slash to get an array of substrings. The array contains the month, day of the month and year.

We used array destructuring to directly assign the array elements to variables.

And then, we passed the values to the Date() constructor.

Note that the month parameter is a zero-based value from 0 (January) to 11 (December), so we had to subtract 1 from the value we got from the date string.

Once you manage to create a Date object from the date string, getting a timestamp is as easy as calling the getTime() method.

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

index.js
const dateStr = '06/24/2022 09:30:05'; const [dateRelated, timeRelated] = dateStr.split(' '); console.log(dateRelated); // 👉️ "06/24/2022" console.log(timeRelated); // 👉️ "09:30:05" const [month, day, year] = dateRelated.split('/'); const [hours, minutes, seconds] = timeRelated.split(':'); const date2 = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); console.log(date2); // 👉️ Fri Jun 24 2022 09:30:05 // 👇️ Get timestamp const timestamp = date2.getTime();
We first had to split the date and time on the space, to get the date and time-related strings in an array.

Then, we repeated the steps from the previous example to get all of the values we need to pass as parameters to the Date() constructor.

Use the search field on my Home Page to filter through my more than 1,000 articles.