1

I usually use Date.now() to get a timestamp. But lately I had to construct a Date object and learn all the methods. I found that .getTime() returns the same thing as Date.now(). So then whats the point of getTime()? Also, why does this work +new Date <-- no () after Date and + before new. And why does it ALSO return the timestamp?

SharedRory
  • 246
  • 6
  • 17
  • Does this answer your question? [Performance - Date.now() vs Date.getTime()](https://stackoverflow.com/questions/12517359/performance-date-now-vs-date-gettime) – Dai Sep 28 '21 at 00:47
  • 1
    @Dai It does not. It doesn't explain why and how +new Date works. Thank you for helping – SharedRory Sep 28 '21 at 00:49
  • `+new Date()` just uses `Date`'s implicit conversion to `number`, which is the same as `getTime()`. – Dai Sep 28 '21 at 00:50
  • 1
    @Dai Thank you for explaining but I also want to know why it works without () – SharedRory Sep 28 '21 at 00:51
  • 3
    If you think _that's_ confusing, check this out... `new Array() instanceof Array === true; Array() instanceof Array === true; new Date() instanceof Date === true; Date() instanceof Date === false` – Lionel Rowe Sep 28 '21 at 01:26

2 Answers2

3

Date.now() gives you the timestamp right now.

Date.getTime() gives you the timestamp of a date object that may represent a moment in the past or future.

+new Date is a slightly confusing way of saying "create a new Date object, then convert it to a number" (identical to new Number(new Date()), and when you convert a Date to a number, it returns the date's timestamp (i.e. getTime().

Use #1 if you want the timestamp right this second, and use #2 if you want to get the timestamp of a Date object you got from somewhere else. Don't use #3 ever - it might look clever, but all you're doing is writing code that's going to confuse yourself or someone else later.

(Side point, the reason new Date works is because you don't actually need parentheses to create an object if you are passing no parameters. You should still use empty parameters though for readability if nothing else.)

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • 1
    Thank you for the explanation. Is there a reason why String(new Date()) returns the same things as the date's toString method but there is no .toNumber method but still + or Number knows how to convert it into a number? – SharedRory Sep 28 '21 at 00:56
  • @SharedRory the equivalent would probably be `Number(new date)` – fabian Sep 28 '21 at 01:01
  • @SharedRory Because `toNumber` isn't very descriptive. The `Date` object has been coded so that when something tries to convert it to a number, it returns the result of `getTime()`, but it would make no sense to call a function called `toNumber` because what would that even mean? – Abion47 Sep 28 '21 at 01:02
  • 1
    @purple Sorry I meant there is no .toNumber method on the date yet Number() knows how to convert it to a date. How? – SharedRory Sep 28 '21 at 01:02
  • @purple `new Date` and `new Date()` are the same thing. – Abion47 Sep 28 '21 at 01:03
  • @Abion47 oh so it is an engine level implementation? – SharedRory Sep 28 '21 at 01:03
  • @SharedRory `Date` has defined the property `[Symbol.toPrimitive](hint)`, which is the function that controls when an object is converted to a primitive. In this specific example, converting `Date` to a number will call that property with the parameter "number", and `Date` takes it from there. – Abion47 Sep 28 '21 at 01:04
  • @Abion47 "new Date and new Date() are the same thing", i never said that they aren't :) – fabian Sep 28 '21 at 01:06
  • @Abion47 That is super cool. Do you have any resources where I can find more info on Symbol.toPrimitive? – SharedRory Sep 28 '21 at 01:07
  • @purple You said the equivalent would be `new Date`, which implies that it's not `new Date()`. If they are the same thing, they are both correct, but the latter is easier for most people to understand. – Abion47 Sep 28 '21 at 01:07
  • @SharedRory You can check out https://javascript.info/object-toprimitive – Abion47 Sep 28 '21 at 01:08
  • @Abion47 i tried to state that the equivalent to String(new Date()) in terms of instead of converting the date object to a string to a number would be `Number(new date)` or `Number(new date())`, which are both the same – fabian Sep 28 '21 at 01:09
  • @SharedRory try to run this example `Number({[Symbol.toPrimitive]() {return 10}})`. As you can see, you can replicate that by yourself. Just crete a Object with the `Symbol.toPrimitive` symbol and try to convert it to a number. Here you can also use the unary plus operator `+{[Symbol.toPrimitive]() {return 10}}` – fabian Sep 28 '21 at 01:11
1

Like this post already explained (Performance - Date.now() vs Date.getTime()), the difference between Date.now() and Date.getTime() are mostly just performance, one comment also states that 'Date.now() doesn’t work in Internet Explorer versions earlier than IE9'.

The method with the unary plus operator, so +Date.now works because the unary plus operator trys to convert something to a number. You can achiev the same with using Number(new Date). The parentheses are not necessary because you are not passing any arguments.

See here for more information on why this works that way: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive

fabian
  • 268
  • 3
  • 15