Date.parse()转化日期为时间戳,ios与Android兼容写法

把固定格式日期转化为时间戳:

//格式化当地日期
new Date(2017-11-11 0:0:0)
//结果为:Sat Nov 11 2017 00:00:00 GMT+0800 (中国标准时间)


//日期转化为当地时间戳
Date.parse(new Date(2017-11-11 0:0:0))
//结果为:1510329600000

<font color="Red">//但是在ios系统下不能解析转化</font>

兼容写法:

//ios下写法:
Date.parse(new Date(2017/11/11 0:0:0))

//兼容写法
Date.parse(new Date(2017-11-11 0:0:0))||Date.parse(new Date(2017/11/11 0:0:0))

//封装函数(data格式为2017-11-11)
function formatTimeStamp(date,time=0:0:0){
    return Date.parse(new Date(`${data} ${time}`))||Date.parse(new Date(`${data.replace(/-/g,/)} ${time}`))
}
formatTimeStamp(2017-11-11);//已经兼容

 

Date.parse()转化日期为时间戳,ios与Android兼容写法

上一篇:安卓六大布局


下一篇:2、获取APP CPU占用率