欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS計算上次日期距離現(xiàn)在多久的代碼

 更新時間:2016年11月21日 16:05:03   作者:Jaycee麥子  
這篇文章主要為大家詳細介紹了iOS計算上次日期距離現(xiàn)在多久的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS上次日期距離現(xiàn)在多久的計算代碼,供大家參考,具體內(nèi)容如下

/**
 * 計算上次日期距離現(xiàn)在多久
 *
 * @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"]);

 輸出結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iOS中使用對象的弱引用示例代碼

    iOS中使用對象的弱引用示例代碼

    這篇文章主要給大家介紹了關于iOS中使用對象的弱引用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01
  • IOS App圖標和啟動畫面尺寸詳細介紹

    IOS App圖標和啟動畫面尺寸詳細介紹

    這篇文章主要介紹了IOS App圖標和啟動畫面尺寸詳細介紹的相關資料,需要的朋友可以參考下
    2017-02-02
  • iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析

    iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析

    這篇文章主要介紹了iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • IOS安裝CocoaPods詳細教程

    IOS安裝CocoaPods詳細教程

    這篇文章主要為大家詳細介紹了IOS安裝CocoaPods教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • IOS中判斷卡頓的方案總結(jié)

    IOS中判斷卡頓的方案總結(jié)

    這篇文章主要介紹了IOS中判斷卡頓的方案總結(jié),對IOS性能感興趣的同學,一定要看一下
    2021-04-04
  • iOS實現(xiàn)步驟進度條功能實例代碼

    iOS實現(xiàn)步驟進度條功能實例代碼

    這篇文章主要給大家介紹了關于iOS實現(xiàn)步驟進度條功能的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • iOS實現(xiàn)視頻壓縮上傳實例代碼

    iOS實現(xiàn)視頻壓縮上傳實例代碼

    本篇文章主要介紹了iOS實現(xiàn)視頻壓縮上傳實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS11 WKWebView 無法加載內(nèi)容的解決方法

    iOS11 WKWebView 無法加載內(nèi)容的解決方法

    這篇文章主要介紹了iOS11 WKWebView 無法加載內(nèi)容,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • iOS兩丫技術之UILabel性能不夠的解決方法

    iOS兩丫技術之UILabel性能不夠的解決方法

    這篇文章主要介紹了iOS中控件UILabel性能不夠而自定義UILabel的過程,UILable是iPhone界面最基本的控件,主要用來顯示文本信息,下面通過本文我們來了解一下
    2022-08-08
  • iOS實現(xiàn)圓角箭頭矩形的提示框

    iOS實現(xiàn)圓角箭頭矩形的提示框

    不知道大家發(fā)現(xiàn)了沒,在現(xiàn)在的很多App中常使用圓角箭頭矩形, 如微博分組提示框, 地圖坐標顯示點等。iPad 中有 UIPopoverController 類供開發(fā)使用, iPhone中就需要開發(fā)人員定制了。那么下面這篇文中就來聊聊定制圓角箭頭矩形提示框,有需要的朋友們可以參考借鑒。
    2016-11-11

最新評論