最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between Date.now(), new Date().getTime(), and +new Date - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question asked Sep 28, 2021 at 0:43 user16238654user16238654 5
  • 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 uses Date's implicit conversion to number, which is the same as getTime(). – 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论