0

I've always wanted to know the difference between the two popular ways to get current timestamp in JS.

Date.now()
new Date().getTime()

What is the difference between the two? And if they are the same, why do we have two things that do the same job?

kanuos
  • 985
  • 9
  • 16
  • 3
    They do the same thing. The first one is just a shortcut when you want the time of now instead of some arbitrary `Date` object. – Barmar Dec 16 '19 at 18:48
  • 3
    Does this answer your question? [Performance - Date.now() vs Date.getTime()](https://stackoverflow.com/questions/12517359/performance-date-now-vs-date-gettime) – Dortimer Dec 16 '19 at 18:49
  • @Dortimer yep its helpful. Din know the .now way we 2x faster. Thanks – kanuos Dec 16 '19 at 20:26

1 Answers1

0

This question was already answered here: https://stackoverflow.com/a/12517413/4052745.
In simple terms they are basically the same thing, but it is suggested to use Date.now() for both a slight advantage in performance as well as simpler syntax.

Matt C.
  • 2,330
  • 3
  • 22
  • 26