JavaScript Date()与Safari和非Safari实现不一致 [英] JavaScript Date() inconsistence with Safari's and non-Safari implementation

查看:40
本文介绍了JavaScript Date()与Safari和非Safari实现不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下琐碎的JS代码:

I have following trivial JS code:

document.getElementById("testLastTime").innerHTML =新日期('2020-07-01T11:59:45').toLocaleString()

在Chrome,Edge,Firefox(所有Windows上)上显示正确的日期:

On Chrome, Edge, Firefox (all on Windows) is shows correct date:

2020年7月1日,上午11:59:45

但是,在iOS设备上,此日期被解释为UTC并显示以下日期(我在PST上):

However on iOS devices this date is interpreted as UTC and following date is shown (I am on PST):

2020年7月1日上午4:59:45

此代码是较大项目的一部分,其中服务器(Python烧瓶)将本地时间的日期时间ISO格式的字符串(不带TZ)发送给客户端.

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).

在所有设备上显示相同日期的JS应该怎么做?

What should I do have JS on all devices show the same date?

推荐答案

如Chief所建议,使用瞬间库解决了该问题.由于我正在使用ChartJS,因此时刻已经加载.

as Chief has suggested using moment libary fixed the issue. Since I am using ChartJS, moment was loaded already.

所以这可行:

document.getElementById ("testLastTime").innerHTML=new moment ('2020-07-01T11:59:45').format ('M/D/YYYY, h:mm:ss A');

我仍然很想知道为什么JS iOS结果使用UTC,而PC结果却不使用Date

I am still curious to understand why JS iOS results are in UTC, while PC results are not while using Date

这篇关于JavaScript Date()与Safari和非Safari实现不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆