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?
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?
- Does this answer your question? Performance - Date.now() vs Date.getTime() – Dai Commented Sep 28, 2021 at 0:47
- 1 @Dai It does not. It doesn't explain why and how +new Date works. Thank you for helping – user16238654 Commented Sep 28, 2021 at 0:49
-
+new Date()
just usesDate
's implicit conversion tonumber
, which is the same asgetTime()
. – Dai Commented Sep 28, 2021 at 0:50 - 1 @Dai Thank you for explaining but I also want to know why it works without () – user16238654 Commented Sep 28, 2021 at 0: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