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?
Asked
Active
Viewed 45 times
0
-
1Does this answer your question? [How to convert date to timestamp?](https://stackoverflow.com/questions/9873197/how-to-convert-date-to-timestamp) – Jorge Guerreiro Mar 02 '22 at 15:34
2 Answers
0
Using .getTime()
you can covert a Date object to a timestamp in JavaScript:
let date = new Date("02-03-2022");
console.log(date.getTime());

Samball
- 623
- 7
- 22
-
While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Mar 03 '22 at 12:47
0
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.

kshetline
- 12,547
- 4
- 37
- 73