How to convert a Date to a Timestamp using JavaScript

Convert a Date to a Timestamp using JavaScript

To convert a date to a timestamp using JavaScript, use the getTime() method. e.g. new Date().getTime(). The getTime() method returns the number of milliseconds that have elapsed since the date (given as a parameter) and the 1st of January, 1970.

The getTime() method returns the number of milliseconds elapsed between the 1st of January, 1970
The getTime() method returns the number of milliseconds elapsed between the 1st of January, 1970

let’s see an example –

const str = '2022-10-04';
const date = new Date(str);

// ✅ Get timestamp in Milliseconds
const timestamp = date.getTime();
console.log(timestamp);

// ✅ Convert Milliseconds to Seconds
const timestampSeconds = Math.floor(date.getTime() / 1000);
console.log(timestampSeconds); 
timestamp in Milliseconds and Convert Milliseconds to Seconds
Timestamp in Milliseconds and Convert Milliseconds to Seconds

This code is creating a new date object from a string, and then getting the timestamp in milliseconds and converting it to seconds.

ℹ️ 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.

The getTime() method returns the number of milliseconds since the ECMAScript epoch which is 1st of January, 1970 00:00:00.

Remember that the time value in a Date object is in UTC, but the methods to fetch the date and time or its components use the local time zone and offset.

To convert milliseconds to seconds, divide by 1000 (1000 milliseconds is equal to 1 second).

const str = '2022-10-04';
const date = new Date(str);

// ✅ Convert Milliseconds to Seconds
const timestampSeconds = Math.floor(date.getTime() / 1000);
console.log(timestampSeconds); 
Convert Milliseconds to Seconds.
Convert Milliseconds to Seconds.

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

To create a Date object, you can pass 2 types of parameters to the Date() constructor:

  1. To create a valid ISO 8601 string, format the string as YYYY-MM-DDTHH:mm:ss.sssZ, or just YYYY-MM-DD if you only have a date without time.
Convert date to ISO 8601 string.
Convert date to ISO 8601 string.

Note -The toISOString() method returns a string in the ISO 8601 format, which is always 24 or 27 characters long. The timezone is always zero UTC offset, as denoted by the suffix Z.

  1. Provide the year, month, day of the month, hours, minutes, and seconds as comma-separated parameters to represent the date and time.

This is an example of how you can take a string, split it up, and then use those parameters to create a Date() constructor or Date object.

// Formatted as MM/DD/YYYY
const str = '10/04/2022';

const [month, day, year] = str.split('/');

const date = new Date(+year, month - 1, +day);
console.log(date);

// ✅ Get timestamp
const timestamp = date.getTime();
console.log(timestamp);
Formatted date as MM-DD-YYYY
Formatted date as MM-DD-YYYY

We have a date string that is formatted as MM/DD/YYYY. To get an array of substrings containing the month, day, and year, we need to split the string on each forward slash.

To create a valid Date object, we pass the values as the first 3 parameters to the Date() constructor. To get a timestamp, call the getTime() method.

When creating a Date object, the month value should be one less than the actual month value. This is because the Date constructor uses a zero-based system, where January = 0, February = 1, March = 2, etc.

This example creates a Date that also contains the hours, minutes and seconds and gets its timestamp.

// 👇️ Formatted as MM/DD/YYYY hh:mm:ss
const str = '10/04/2022 03:20:30';

const [dateComponents, timeComponents] = str.split(' ');
console.log(dateComponents); 
console.log(timeComponents);

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

// ✅ Get timestamp
const timestamp = date.getTime();
console.log(timestamp); 
Creates a Date that also contains the hours, minutes and seconds and gets its timestamp.
Creates a Date that also contains the hours, minutes and seconds and gets its timestamp.

Explanation – First, we split the date and time string on the space to get the date and time components as separate strings. Then, we split the time string on each colon and assigned the hours, minutes and seconds to variables. Finally, we passed all of the parameters to the Date() constructor to create a Date object and got the timestamp by calling the getTime() method.

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

How to Convert a Date to a Timestamp using JavaScript.