zoukankan      html  css  js  c++  java
  • 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(@"
    
    result: %@", [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 输出结果如下:

  • 相关阅读:
    javaScript 闭包
    Netty入门1之----认识Netty
    浅谈 @RequestParam 和@PathVariable
    分布式事务的解决方案
    java实现哈弗曼树和哈夫曼树压缩
    深入理解mysql的底层实现
    深入理解http协议的特点
    char和varchar的区别
    详解聚簇索引
    解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
  • 原文地址:https://www.cnblogs.com/rglmuselily/p/6084685.html
Copyright © 2011-2022 走看看