I’ve run into these 2 expressions and and confused …
" new Date(“25 July 2016”).getTime() !== new Date(“2016-07-25”).getTime()
new Date("2016-07-25").getTime() === new Date("2016-07-
So basically the second expression evaluates to true because(I think) the left sideit creates a ISO timestamp by default.
Assuming I am understanding this correctly:
Why is the first expression evaluating to false?
new Date(“25 July 2016”).getTime()
Will give you local time (same date but local time offset)
Will give you UTC time.
It is an implementation-specific issue (different browsers may do different things). In all current implementations it will, as of now (2020), always parse the second one as expected (UTC time), first one – it should be consistent across all contemporary implementations, but I don’t know that for sure.
If you want the second to be local, it needs to be longform, ie you need to specify the time and timezone offset (for example, to use local system time, as the first one does automatically, 2016-07-25T00:00:00Z)
Date is not good, there are a lot of little issues like this that have built up over the years. It works for the most part, and is normally more than usable, but you need to be careful with it. There is a new API (
Temporal) that hopefully should remove the need to use
Date in most cases, but even if its API gets finalised it’s a year or two off being finished and in browsers I think.