具有字符串值的 Safari 新日期超出不同的时间

Safari new Date with string value outs different time

我想做的是将 "yyyy-mm-dd HH:mm:ss" 字符串更改为日期值。

这是当前代码

var c = new Date('2019-01-19 23:59:59'.replace(/\s+/g, 'T'))

它returns

我应该怎么做才能使它 returns 都在同一天?

谢谢。

将“Z”添加到 GMT/UTC 时区的 date 字符串中

var c = new Date('2019-01-19 23:59:59'.replace(/\s+/g, 'T')+'Z');

ISO 日期可以加上小时、分钟和秒 (YYYY-MM-DDTHH:MM:SSZ): 日期和时间用大写字母 T 分隔。

UTC 时间定义为大写字母 Z。

如果您想修改相对于 UTC 的时间,请删除 Z 并改为添加 +HH:MM 或 -HH:MM:

例如 var d = new Date("2019-01-19T23:59:59-09:00");

Safari...在使用日期字符串创建实例时不考虑时区偏移。

在末尾加上Z也不错,但如果你想得到与其他浏览器相同的结果,应该计算时区偏移。

这是我所做的...

// Before do this, check navigator.userAgent 
// and execute below logic if it is desktop Safari.

// Add Z is the convention, but you won't get any error even if do not add.
var c = new Date('2019-01-19 23:59:59Z'.replace(/\s+/g, 'T')) 

// It will returns in minute
var timeOffset = new Date().getTimezoneOffset();

// Do not forget getTime, if not, you will get Invalid date         
var d = new Date(c.getTime() + (timeOffset*60*1000))    

将把这个post打开到明天等待更好的答案。 谢谢