带有字符串值的Safari新日期超出了其他时间 [英] Safari new Date with string value outs different time

查看:42
本文介绍了带有字符串值的Safari新日期超出了其他时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What I am trying to do is changing "yyyy-mm-dd HH:mm:ss" string to date value.

这是当前代码

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

返回


  • chrome: 2019年1月19日星期六23:59:59 GMT + 0900(KST)

  • 野生动物园: 2019年1月20日星期日格林尼治标准时间+0900(KST)

  • ie11:星期六1月19 2019 23:59:59 GMT + 0900(KST)

  • chrome : Sat Jan 19 2019 23:59:59 GMT+0900 (KST)
  • safari : Sun Jan 20 2019 08:59:59 GMT+0900 (KST)
  • ie11 : Sat Jan 19 2019 23:59:59 GMT+0900 (KST)

我应该怎么做返回所有相同的日期?

What should I do to make it returns all same date?

谢谢。

推荐答案

Safari。 。创建带有日期字符串的实例时,它不考虑时区偏移。

Safari... It does not consider time zone offset when creates an instance with date string.

最后添加 Z 也是不错的选择,但是如果您想在其他浏览器上获得相同的结果,应计算时区偏移量。

Add Z to the end is also good point, but if you want to get the same result with other browsers, should calculate timezone offset.

这是我所做的...

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

将打开此帖子,直到明天等待更好的答案。
谢谢。

Will open this post till tomorrow for waiting better answer. Thanks.

这篇关于带有字符串值的Safari新日期超出了其他时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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