iOS - 新浪微博API發(fā)布微博時(shí)間返回值與當(dāng)前時(shí)間對(duì)比,實(shí)現(xiàn)幾天前,幾小時(shí)前,幾分鐘前

iOS - 新浪微博API發(fā)布微博時(shí)間返回值與當(dāng)前時(shí)間對(duì)比,實(shí)現(xiàn)幾天前,幾小時(shí)前,幾分鐘前

這個(gè)問題在度娘上搜了好半天都沒有搜到,好不容易搜到復(fù)制代碼到項(xiàng)目中一測(cè)試就出問題了,的出來值相差太遠(yuǎn),為了解決這個(gè)問題,我是絞盡腦汁的想辦法,查問題出在哪里,浪費(fèi)了好多時(shí)間,終于找到問題所在了,原來是當(dāng)前時(shí)間在對(duì)比過去時(shí)間時(shí)出來問題。

網(wǎng)上復(fù)制的代碼:

+ (NSString *) compareCurrentTime:(NSString *)str
{

    //把字符串轉(zhuǎn)為NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate *timeDate = [dateFormatter dateFromString:str];

    //得到與當(dāng)前時(shí)間差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //標(biāo)準(zhǔn)時(shí)間和北京時(shí)間差8個(gè)小時(shí)
    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"剛剛"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%d分鐘前",temp];
    }

    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%d小時(shí)前",temp];
    }

    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%d天前",temp];
    }

    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%d月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%d年前",temp];
    }

    return  result;
}

既然知道了問題所在,這下就好解決了!

我針對(duì)當(dāng)前時(shí)間在對(duì)比過去時(shí)間的問題,去搜了一下都有哪些對(duì)比時(shí)間的代碼。知道問題所在才能對(duì)癥下藥

錯(cuò)誤代碼:NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow] ;

這個(gè)錯(cuò)處在了[timeDate timeIntervalSinceNow]上,不可以這寫,這樣寫得出時(shí)間差的值非常大,而且還是負(fù)值

所以我就用了下面的寫法

正確代碼:NSTimeInterval  timeInterval = [date2 timeIntervalSinceDate:timeDate];

就是把當(dāng)前時(shí)間和過去時(shí)間去作對(duì)比,[date2 timeIntervalSinceDate:timeDate];這樣還是比較好的,這樣出偏差的幾率很小

本以為這樣就可以了,可是運(yùn)行后才發(fā)現(xiàn)還有問題存在,然后我就從獲取時(shí)間差下面開始找,問題是在NSTimeInterval timeInterval = [date2 timeIntervalSinceDate:timeDate]; 這行代碼下面的兩行代碼中出錯(cuò)的

錯(cuò)誤代碼:

timeInterval = -timeInterval;

timeInterval = timeInterval - 8*60*60;

就是這兩行代碼,從這兩行代碼中打印輸出后,發(fā)現(xiàn)所得出來的值是有問題的

沒辦法又去搜了,可是,沒有得到自己想要的答案。一怒之下就把這兩行代碼給干掉了,運(yùn)行后發(fā)現(xiàn),我靠,好了

刪掉這兩行代碼是對(duì)的,得出了正確的答案了

說了這么多的廢話,我就把正確的代碼貼出來,代碼如下:

+ (NSString *)compareCurrentTime:(NSString *)str
{
    //把字符串轉(zhuǎn)為NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss Z yyyy"];
    
    dateFormatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    NSDate *date2 = [NSDate date];
    //得到與當(dāng)前時(shí)間差
    NSTimeInterval  timeInterval = [date2 timeIntervalSinceDate:timeDate];
    
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"剛剛"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分鐘前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小時(shí)前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
    
}

這就是正確的代碼了,到處結(jié)束了!

希望能幫助到你!

最后編輯于
?著作權(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,039評(píng)論 25 709
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,697評(píng)論 18 399
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 12,413評(píng)論 6 13
  • 最近再忙結(jié)課設(shè)計(jì),整個(gè)人差點(diǎn)被修圖跟建模逼瘋,得虧小伙伴的及時(shí)解救,讓我可以愉快的開始我的暑假生活啦,今天晚上就可...
    陽陽君閱讀 2,097評(píng)論 23 36
  • 哎呦喂,這兩天最火爆的娛樂圈新聞是王寶強(qiáng)宣布離婚的消息了。一時(shí)間全國(guó)人民都知道的趕腳。反正新一輪離婚互撕開始,全民...
    華枝春滿5339閱讀 446評(píng)論 1 1

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