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.
-
5
For Java I remend you don’t use
java.util.Date
. That class is poorly designed and long outdated. Instead useInstant
from java.time, the modern Java date and time API. But yes, both ofInstant.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
orZonedDateTime
. – 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
1 Answer
Reset to default 12Yes, 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
版权声明:本文标题:javascript - Does Date.now() return different value for different timezones? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743880147a2555050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论