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


 1 /**
 2  *  計算上次日期距離現在多久
 3  *
 4  *  @param lastTime    上次日期(需要和格式對應)
 5  *  @param format1     上次日期格式
 6  *  @param currentTime 最近日期(需要和格式對應)
 7  *  @param format2     最近日期格式
 8  *
 9  *  @return xx分鍾前、xx小時前、xx天前
10  */
11 + (NSString *)timeIntervalFromLastTime:(NSString *)lastTime
12                         lastTimeFormat:(NSString *)format1
13                          ToCurrentTime:(NSString *)currentTime
14                      currentTimeFormat:(NSString *)format2{
15     //上次時間
16     NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
17     dateFormatter1.dateFormat = format1;
18     NSDate *lastDate = [dateFormatter1 dateFromString:lastTime];
19     //當前時間
20     NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
21     dateFormatter2.dateFormat = format2;
22     NSDate *currentDate = [dateFormatter2 dateFromString:currentTime];
23     return [Utilities timeIntervalFromLastTime:lastDate ToCurrentTime:currentDate];
24 }
25  
26 + (NSString *)timeIntervalFromLastTime:(NSDate *)lastTime ToCurrentTime:(NSDate *)currentTime{
27     NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
28     //上次時間
29     NSDate *lastDate = [lastTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:lastTime]];
30     //當前時間
31     NSDate *currentDate = [currentTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:currentTime]];
32     //時間間隔
33     NSInteger intevalTime = [currentDate timeIntervalSinceReferenceDate] - [lastDate timeIntervalSinceReferenceDate];
34  
35     //秒、分、小時、天、月、年
36     NSInteger minutes = intevalTime / 60;
37     NSInteger hours = intevalTime / 60 / 60;
38     NSInteger day = intevalTime / 60 / 60 / 24;
39     NSInteger month = intevalTime / 60 / 60 / 24 / 30;
40     NSInteger yers = intevalTime / 60 / 60 / 24 / 365;
41  
42     if (minutes <= 10) {
43         return  @"剛剛";
44     }else if (minutes < 60){
45         return [NSString stringWithFormat: @"%ld分鍾前",(long)minutes];
46     }else if (hours < 24){
47         return [NSString stringWithFormat: @"%ld小時前",(long)hours];
48     }else if (day < 30){
49         return [NSString stringWithFormat: @"%ld天前",(long)day];
50     }else if (month < 12){
51         NSDateFormatter * df =[[NSDateFormatter alloc]init];
52         df.dateFormat = @"M月d日";
53         NSString * time = [df stringFromDate:lastDate];
54         return time;
55     }else if (yers >= 1){
56         NSDateFormatter * df =[[NSDateFormatter alloc]init];
57         df.dateFormat = @"yyyy年M月d日";
58         NSString * time = [df stringFromDate:lastDate];
59         return time;
60     }
61     return @"";
62 }
63  
64 使用如下:
65 NSLog(@"\n\nresult: %@", [Utilities timeIntervalFromLastTime:@"2015年12月8日 15:50"
66                                            lastTimeFormat:@"yyyy年MM月dd日 HH:mm"
67                                             ToCurrentTime:@"2015/12/08 16:12"
68                                         currentTimeFormat:@"yyyy/MM/dd HH:mm"]);
69  
70 輸出結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM