JavaScript Date() inconsistence with Safari's and non-Safari implementation

允我心安 提交于 2021-01-29 20:28:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!