-
Notifications
You must be signed in to change notification settings - Fork 16
Safari 浏览器关于 Date 对象的兼容问题
Robert Yao edited this page Jul 17, 2019
·
1 revision
首先想吐槽一下 Safari 浏览器(PC版)完全就没法用,很多 CSS3 和 ES6 的新特性都不支持。
其次,Safari 浏览器 Date 构造函数获取字符串日期也有坑。比如拿 Calender 配置中的演示示例 '2019-7'或者'2019',在其它浏览器中 new Date('2019-7') 和 new Date('2019') 都可以正确识别,而 Safari 则会报错:“Invalid Date”。真是无语!
在 Safari 中,必须是 new Date('2019/1/1') 或者 new Date('1/1/2019'),new Date('2019/1') 或者 new Date('1/2019')都会报错:“Invalid Date”,所以在Calendar v0.4.1 中特意添加了 toAllSupported 方法:
// 只针对表示日期的字符串处理
const toAllSupported = (time) => {
let date = []
if(isNumber(time)){
return time
} else {
if(isString(time)) {
if (time.indexOf('-')) {
date = time.split('-')
} else {
if (time.indexOf('/')) {
date = time.split('/')
}
}
if (date.length === 1) {
date.push('1')
date.push('1')
} else{
if (date.length === 2) {
// 例如:'2019-1'
if (date[0].length === 4) {
date.push('1')
} else {
// 例如:'1-2019'
if (data[1].length === 4) {
date.unshift('1')
}
}
}
}
return date.join('/')
}
}
}