How to Convert Dates to Timestamps in JavaScript

How to Convert Dates to Timestamps in JavaScript

Ferenc Almasi β€’ 2020 September 12 β€’ Read time 1 min read
  • twitter
  • facebook
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();
timestamp.js

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();
timestamp.js

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.

How to Convert Dates to Timestamps
If you would like to see more Webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.