問題1:微信小程序中iOS系統(tǒng)
識別不了yyyy-mm-dd hh:mm
锈麸,轉(zhuǎn)為yyyy/mm/dd hh:mm
:
new Date("2019-12-05 11:36")
// 當(dāng)new Date中的時間格式為這樣的時候检柬,會返回Invalid Date
正確應(yīng)為:
new Date("2019/12/05 11:36")
問題2:將時間按時區(qū)轉(zhuǎn)化旧蛾,解決真機與開發(fā)者工具時間不一致的bug:
從接口上獲取的時間數(shù)據(jù),需求是要計算這條數(shù)據(jù)的距當(dāng)前時間的倒計時陕凹,思路是new Date(數(shù)據(jù)).getTime() - new Date().getTime(),得到相差的時間戳再轉(zhuǎn)為年月日時分秒萝映。
在微信開發(fā)者工具上,時間是正確的罩阵,但是在真機上展示的與微信開發(fā)者工具相差8h竿秆,最后測試半天加搜資料,發(fā)現(xiàn)new Date在不同終端上獲取的時間可能默認(rèn)的時區(qū)不同(具體原因未知)稿壁。所以在處理數(shù)據(jù)時加上時區(qū)
new Date(數(shù)據(jù)+'+0800')
,達(dá)到在不同終端幽钢,都是按照同個時區(qū)獲取。
封裝了一個處理函數(shù)傅是,解決以上兩個問題:
/**
* 將時間按時區(qū)轉(zhuǎn)化匪燕,解決真機與開發(fā)者工具時間不一致的bug
* ios識別不了yyyy-mm-dd hh:mm蕾羊,轉(zhuǎn)為yyyy/mm/dd hh:mm
*/
fixDate(strTime) {
if (!strTime) {
return '';
}
var tempDate = new Date(strTime+'+0800');
if(tempDate=='Invalid Date'){
strTime = strTime.replace(/T/g,' ');
strTime = strTime.replace(/-/g,'/');
tempDate = new Date(strTime+'+0800');
}
tempDate.toLocaleDateString();
return tempDate;
},
更博客好累??...