iOS之計算上次日期距離現在多久, 如 xx 小時前、xx 分鐘前等

來源:互聯網
上載者:User

標籤:cer   result   oms   日期   return   current   enter   int   技術   

/**   *  計算上次日期距離現在多久   *   *  @param lastTime    上次日期(需要和格式對應)   *  @param format1     上次日期格式   *  @param currentTime 最近日期(需要和格式對應)   *  @param format2     最近日期格式   *   *  @return xx分鐘前、xx小時前、xx天前   */ + ( NSString *)timeIntervalFromLastTime:( NSString *)lastTime                          lastTimeFormat:( NSString *)format1                           ToCurrentTime:( NSString *)currentTime                       currentTimeFormat:( NSString *)format2{      //上次時間      NSDateFormatter *dateFormatter1 = [[ NSDateFormatter alloc]init];      dateFormatter1.dateFormat = format1;      NSDate *lastDate = [dateFormatter1 dateFromString:lastTime];      //目前時間      NSDateFormatter *dateFormatter2 = [[ NSDateFormatter alloc]init];      dateFormatter2.dateFormat = format2;      NSDate *currentDate = [dateFormatter2 dateFromString:currentTime];      return [Utilities timeIntervalFromLastTime:lastDate ToCurrentTime:currentDate]; }  + ( NSString *)timeIntervalFromLastTime:( NSDate *)lastTime ToCurrentTime:( NSDate *)currentTime{      NSTimeZone *timeZone = [ NSTimeZone systemTimeZone];      //上次時間      NSDate *lastDate = [lastTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:lastTime]];      //目前時間      NSDate *currentDate = [currentTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:currentTime]];      //時間間隔      NSInteger intevalTime = [currentDate timeIntervalSinceReferenceDate] - [lastDate timeIntervalSinceReferenceDate];       //秒、分、小時、天、月、年      NSInteger minutes = intevalTime / 60;      NSInteger hours = intevalTime / 60 / 60;      NSInteger day = intevalTime / 60 / 60 / 24;      NSInteger month = intevalTime / 60 / 60 / 24 / 30;      NSInteger yers = intevalTime / 60 / 60 / 24 / 365;       if (minutes <= 10) {          return  @"剛剛" ;      } else if (minutes < 60){          return [ NSString stringWithFormat: @"%ld分鐘前" ,( long )minutes];      } else if (hours < 24){          return [ NSString stringWithFormat: @"%ld小時前" ,( long )hours];      } else if (day < 30){          return [ NSString stringWithFormat: @"%ld天前" ,( long )day];      } else if (month < 12){          NSDateFormatter * df =[[ NSDateFormatter alloc]init];          df.dateFormat = @"M月d日" ;          NSString * time = [df stringFromDate:lastDate];          return time;      } else if (yers >= 1){          NSDateFormatter * df =[[ NSDateFormatter alloc]init];          df.dateFormat = @"yyyy年M月d日" ;          NSString * time = [df stringFromDate:lastDate];          return time;      }      return @"" ; }  使用如下: NSLog ( @"\n\nresult: %@" , [Utilities timeIntervalFromLastTime: @"2015年12月8日 15:50"                                             lastTimeFormat: @"yyyy年MM月dd日 HH:mm"                                              ToCurrentTime: @"2015/12/08 16:12"                                          currentTimeFormat: @"yyyy/MM/dd HH:mm" ]);  輸出結果如下:

iOS之計算上次日期距離現在多久, 如 xx 小時前、xx 分鐘前等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.