Should we just append Z to them on the server side?
TL;DR Yes, you probably should.
Correct handling of dates and date/times without a timezone has, sadly, varied through the years?— both in terms of specification and JavaScript engines adherence to the specification.
When this answer was originally written in 2013, the ES5 spec (the first to have a defined date/time format for JavaScript) was clear: No timezone = UTC:
The value of an absent time zone offset is “Z”.
This was at odds with ISO-8601, which the ES5 date/time format was based on, in which the absense of a timezone indicator means "local time." Some implementations never implemented ES5's meaning, sticking instead to ISO-8601.
In ES2015 (aka "ES6"), it was changed to match ISO-8601:
If the time zone offset is absent, the date-time is interpreted as a local time.
However, this caused incompatibility problems with existing code, particularly with date-only forms like 2018-07-01
, so in ES2016 it was changed yet again:
When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
So new Date("2018-07-01")
is parsed as UTC, but new Date("2018-07-01T00")
is parsed as local time.
It's been consistent since, in ES2017 and in the upcoming ES2018; here's the link to the current draft standard, which also has the text above.
You can test your current browser here:
function test(val, expect) {
var result = +val === +expect ? "Good" : "ERROR";
console.log(val.toISOString(), expect.toISOString(), result);
}
test(new Date("2018-07-01"), new Date(Date.UTC(2018, 6, 1)));
test(new Date("2018-07-01T00:00:00"), new Date(2018, 6, 1));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…