Strange even how it happens. It must be something time zone related.
Anyway, I’d have a lot of trouble if instead of "2018-07-03"
you put "2018-07-03 00:00"
?
That way came out right (at least here)
EDITION 23/07/2019
I know it’s been a while, but anyway. Really the problem is the time zone.
If you use a string with the year, month and day separated with hyphen, the time zone that JS chooses for you is Coordinated Universal Time (UTC), which is the time zone used to calculate the other time zones in the world.
Now if you use bars to separate the year, month and day, the chosen time zone is your local time zone.
I mean, in your case it would have been better to have used "2018/07/03"
.
Ah, and <input type="date">
generates hyphenated values.
Maybe this information will be useful to someone
Source: Exploring Unexpected Behavior With Javascript Date Objects
Put your code, because the way it’s in the question it doesn’t happen...
– novic
Do you need to receive this date as a string? Because an option would be to use the input type Date, which will already return you a Date value
– Nathan Schroeder
So he considers the Timezone and does not bring the correct date, you can specify the time and the Timezone, so:
new Date('2018-07-03T10:15:00')
– Ricardo Pontual
When you use
new Date("2018-07-03")
, the result corresponds to this date (3 July 2018), at midnight on UTC (or2018-07-03T00:00:00Z
).&#The problem is that this same date in UTC may correspond to a different date and time in other timezones. For example, in São Paulo, this same value corresponds to2018-07-02T21:00-03:00
(9 the night of the previous day), and many Date methods use the browser Timezone to interpret the value. How to treat the problem will depend on what you are doing with the date– hkotsubo