ios中g(shù)etTime()的兼容性實(shí)例代碼
時間格式為:2017-12-12 12:00:00在蘋果上獲取時間戳有兼容性問題
需要轉(zhuǎn)換成2017/12/12 12:00:00 才可以正確獲取到時間戳
let u = navigator.userAgent; //判斷瀏覽器型號 let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android終端 let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端 if(isiOS){ let time = new Date((v[0].createTime).replace(/-/g,'/')).getTime(); this.timeago=this.getDateDiff(time); }else{ let time = new Date(v[0].createTime).getTime(); this.timeago=this.getDateDiff(time); }
判斷時間過去了多久 getDateDiff(dateTimeStamp){ let result; let minute = 1000 * 60; let hour = minute * 60; let day = hour * 24; let halfamonth = day * 15; let month = day * 30; let now = new Date().getTime(); let diffValue = now - dateTimeStamp; if(diffValue < 0){ return; } let monthC =diffValue/month; let weekC =diffValue/(7*day); let dayC =diffValue/day; let hourC =diffValue/hour; let minC =diffValue/minute; if(monthC>=1){ if(monthC<=12) result="" + parseInt(monthC) + "月前"; else{ result="" + parseInt(monthC/12) + "年前"; } } else if(weekC>=1){ result="" + parseInt(weekC) + "周前"; } else if(dayC>=1){ result=""+ parseInt(dayC) +"天前"; } else if(hourC>=1){ result=""+ parseInt(hourC) +"小時前"; } else if(minC>=1){ result=""+ parseInt(minC) +"分鐘前"; }else{ result="剛剛"; } return result; },
到此這篇關(guān)于ios中g(shù)etTime()的兼容性實(shí)例代碼的文章就介紹到這了,更多相關(guān)getTime()的兼容性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iOS應(yīng)用運(yùn)用設(shè)計(jì)模式中的Strategy策略模式的開發(fā)實(shí)例
這篇文章主要介紹了iOS應(yīng)用開發(fā)中對設(shè)計(jì)模式中的Strategy策略模式的運(yùn)用,例子采用傳統(tǒng)的Objective-C語言代碼演示,需要的朋友可以參考下2016-03-03IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場動畫詳解
這篇文章主要介紹了IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場動畫,CAAnimation的子類,用于做轉(zhuǎn)場動畫,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭赢嬓Ч?,感興趣的小伙伴們可以參考一下2016-02-02iOS Objective-c實(shí)現(xiàn)左右滑動切換頁面
這篇文章主要為大家詳細(xì)介紹了iOS Objective-c實(shí)現(xiàn)左右滑動切換頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08IOS 解決推送本地國際化 loc-key 本地化失敗的問題
本文主要介紹IOS 推送國際化問題,在開發(fā) IOS 項(xiàng)目過程中對軟件的國際化有的項(xiàng)目需求是需要的,這里給大家一個示例,有需要的小伙伴可以參考下2016-07-07