How to Convert Dates to Timestamps in JavaScript
You can simply convert dates to timestamp in JavaScript by prepending them with a + sign:
Copied to clipboard! Playground
// Quickly convert dates to timestamp with:
// Returns: 1583318177897
+new Date();
// Returns: Wed Mar 04 2020 11:36:20 GMT+0100
new Date();
This is essentially typecasting the Date into a number. It is equivalent of doing:
Copied to clipboard!
// This will return 0
new Date('1970-01-01').getTime();
Make sure you pass in a date in the above format. If you would like to know how to also format your dates, make sure to checkout this other webtip.
Resources:
π More Webtips
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: