问题
I have following trivial JS code:
document.getElementById ("testLastTime").innerHTML=new Date ('2020-07-01T11:59:45').toLocaleString()
On Chrome, Edge, Firefox (all on Windows) is shows correct date:
7/1/2020, 11:59:45 AM
However on iOS devices this date is interpreted as UTC and following date is shown (I am on PST):
7/1/2020 4:59:45 AM
This code is part of the larger project, where datetime ISO formated string in local time (without TZ) is send to client by the server (Python flask).
What should I do have JS on all devices show the same date?
回答1:
as Chief has suggested using moment libary fixed the issue. Since I am using ChartJS, moment was loaded already.
so while this works:
document.getElementById ("testLastTime").innerHTML=new moment ('2020-07-01T11:59:45').format ('M/D/YYYY, h:mm:ss A');
I am still curious to understand why JS iOS results are in UTC, while PC results are not while using Date
来源:https://stackoverflow.com/questions/62683906/javascript-date-inconsistence-with-safaris-and-non-safari-implementation