admin管理员组

文章数量:1352882

The definition for Date.now() is not clear for me. As per definition "The Date. now() is an inbuilt function in JavaScript which returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.". So, does it mean that it will give same value for Date.now() in all timezone?
The current date and time, picked for calculation, is my local timezone or UTC ? I have same query for java.util.Date getTime() method.

The definition for Date.now() is not clear for me. As per definition "The Date. now() is an inbuilt function in JavaScript which returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.". So, does it mean that it will give same value for Date.now() in all timezone?
The current date and time, picked for calculation, is my local timezone or UTC ? I have same query for java.util.Date getTime() method.

Share Improve this question edited Mar 22, 2020 at 13:29 Anonymous 86.6k15 gold badges162 silver badges178 bronze badges asked Mar 22, 2020 at 13:13 ASHISH KARNASHISH KARN 1311 silver badge7 bronze badges 5
  • 5 For Java I remend you don’t use java.util.Date. That class is poorly designed and long outdated. Instead use Instant from java.time, the modern Java date and time API. But yes, both of Instant.now() and (new Date()).getTime() return the same result regardless of time zone. – Anonymous Commented Mar 22, 2020 at 13:16
  • 3 In Java as of 8, you should use either LocalDateTime or ZonedDateTime. – Nikolas Commented Mar 22, 2020 at 13:17
  • Is this for Java or for JavaScript? Do you know that javascriptisnotjava. ? Fix your tags. – Basil Bourque Commented Mar 22, 2020 at 19:16
  • 1 "Date.now()" in javascript and "new Date().getTime()" in Java. I had doubt for both the languages and both have been answered here. So, tags are correct. – ASHISH KARN Commented Mar 23, 2020 at 2:57
  • can you rephrase the question so the answer 'Yes" matches? i.e. "Does Date.now() return the same result independent of timezone?" – KHB Commented Jul 14, 2021 at 5:36
Add a ment  | 

1 Answer 1

Reset to default 12

Yes, Date.now() will give you the same UTC timestamp independent of your current timezone. Such a timestamp, rather a point in time, does not depend on timezones.

The Java equivalent new Date() gives you the exact same thing.

Check out Coordinated Universal Time (UTC) for more information.


FYI: Don't use new Date() in Java as it's a legacy class. Use Instant.now() that is from the new java.time API that is much more robust and has a nicer design.

本文标签: javascriptDoes Datenow() return different value for different timezonesStack Overflow