IOS solve the new Date () time format is not compatible micro letter applet

This week write small programs, encounter a bug, have displayed good time on the chrome, one to Safari / iPhone on the error "invalid date", the time format is "2019.06.06 13:12:49", and then use the new Date () conversion timestamp, using micro-channel development tools, security Developer Edition mobile phone, mobile phone security trial version no problem, ios can not show.

Suspect, will be Safari does not support yyyy-mm-dd / yyyy.mm.dd format, so the safari browser testing wave, by the way also tested "2018-12-10" format:

safari browser error: 2018.12.10 11:11: 11 period format

safari browser error: 2018-12-10 11:11:11 Date Format

So they replace regular replacement

let dateStr1 = '2018.12.10 11:11:11';
let dateStr2 = '2018-12-10 11:11:11';
/* 利用正则表达式替换时间中的”-或者.”为”/”即可 */
dateToTimestamp(dateStr) {
    if (!dateStr) {
        return ''
    }
    let newDataStr = dateStr.replace(/\.|\-/g, '/')
    let date = new Date(newDataStr);
    let timestamp = date.getTime();
    return timestamp
}

this.dateToTimestamp(dateStr1)
this.dateToTimestamp(dateStr2)
复制代码

Later, in order to verify their ideas on the stackoverflow look up and see a few similar issues here pick a representative for everyone to see:

Safari JS cannot parse YYYY-MM-DD date format?

Probably it means that, in execution new Date( string )time, different browsers use different parse, currently chrometwo formats are supported, but Safarionly supports yyyy / mm / dd.

PS: recently started to develop the mobile side, the back should encounter a lot of compatibility problems, constantly sum up, hope to step on little pit!

Reproduced in: https: //juejin.im/post/5cf8d3baf265da1b916383f3

Guess you like

Origin blog.csdn.net/weixin_33890526/article/details/91466407