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
Mentoring

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:

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.