'How to convert date into timestamp with javascript

I have a date in YYYY-MM-DD format and I want to convert it into a timestamp like 1645985084088 using javascript. Can someone help me out?



Solution 1:[1]

To ensure consistency regardless of the timezone of the user:

let date = '2022-03-02';
new Date(date + 'T00:00Z').getTime();

This will give the UTC timestamp for midnight at the start of the given date.

Solution 2:[2]

Using .getTime() you can covert a Date object to a timestamp in JavaScript:

let date = new Date("02-03-2022");
console.log(date.getTime());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kshetline
Solution 2