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.
Asked
Active
Viewed 2,368 times
2

Ole V.V.
- 81,772
- 15
- 137
- 161

ASHISH KARN
- 91
- 6
-
5For Java I recommend 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](https://docs.oracle.com/javase/tutorial/datetime/). But yes, both of `Instant.now()` and `(new Date()).getTime()` return the same result regardless of time zone. – Ole V.V. Mar 22 '20 at 13:16
-
3In Java as of 8, you should use either `LocalDateTime` or `ZonedDateTime`. – Nikolas Charalambidis Mar 22 '20 at 13:17
-
Is this for Java or for JavaScript? Do you know that http://javascriptisnotjava.com ? Fix your tags. – Basil Bourque Mar 22 '20 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 Mar 23 '20 at 02:57
-
can you rephrase the question so the answer 'Yes" matches? i.e. "Does Date.now() return the _same_ result independent of timezone?" – K.H. B Jul 14 '21 at 05:36
1 Answers
10
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.

akuzminykh
- 4,522
- 4
- 15
- 36