iOS 時(shí)間戳轉(zhuǎn)換為幾分鐘前

不說(shuō)廢話,項(xiàng)目中有用到過(guò)這個(gè)。后臺(tái)傳過(guò)來(lái)的13位純數(shù)字時(shí)間戳(createTimeString)

實(shí)現(xiàn)代碼

- (NSString *)updateTimeForRow:(NSString *)createTimeString {
    // 獲取當(dāng)前時(shí)時(shí)間戳 1466386762.345715 十位整數(shù) 6位小數(shù)
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    // 創(chuàng)建歌曲時(shí)間戳(后臺(tái)返回的時(shí)間 一般是13位數(shù)字)
    NSTimeInterval createTime = [createTimeString longLongValue]/1000;
    // 時(shí)間差
    NSTimeInterval time = currentTime - createTime;
    
    NSInteger sec = time/60;
    if (sec<60) {
        return [NSString stringWithFormat:@"%ld分鐘前",sec];
    }
    
    // 秒轉(zhuǎn)小時(shí)
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld小時(shí)前",hours];
    }
    //秒轉(zhuǎn)天數(shù)
    NSInteger days = time/3600/24;
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld天前",days];
    }
    //秒轉(zhuǎn)月
    NSInteger months = time/3600/24/30;
    if (months < 12) {
        return [NSString stringWithFormat:@"%ld月前",months];
    }
    //秒轉(zhuǎn)年
    NSInteger years = time/3600/24/30/12;
    return [NSString stringWithFormat:@"%ld年前",years];
}

原文: iOS 時(shí)間戳轉(zhuǎn)換為幾分鐘前等

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,115評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,410評(píng)論 4 61
  • 有句老話怎么說(shuō)來(lái)著,合伙生意不好做,千萬(wàn)不能和自己的好朋友做生意,免得落得朋友沒得做,錢也沒掙著。 老實(shí)說(shuō),我一直...
    胖胖的黑閱讀 251評(píng)論 1 1
  • 多情少年時(shí) 2002年,時(shí)別三年。尚淺13歲,高楓23歲。 尚淺正讀初中,高楓成了尚淺的語(yǔ)文老師兼班主任。只是尚淺...
    夏言baby閱讀 1,143評(píng)論 0 1

友情鏈接更多精彩內(nèi)容