Trusted answers to developer questions

How to get the timestamp in JavaScript

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

The timestamp is the number of milliseconds elapsed since January 1, 1970. It is also known as the Unix time.

svg viewer

We can get this value using methods of the JavaScript date object:

  1. Use the valueOf() method (or + operator which invokes the valueOf() method):
console.log(new Date().valueOf());
   OR
console.log(+new Date());
  1. Use the now() method. This way, a Date object will not be created:
console.log(Date.now());

Example:

console.log(new Date().valueOf());
console.log(+new Date());
console.log(Date.now());

RELATED TAGS

timestamp
javascript
get
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?