星空网 > 软件开发 > 操作系统

iOS之计算上次日期距离现在多久, 如 xx 小时前、xx 分钟前等

/**
 *  计算上次日期距离现在多久
 *
 *  @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 分钟前等images/loading.gif' data-original="http://code4app.com/data/attachment/forum/201605/27/175323rwa1mls5gmlui3zr.png" width="442">





原标题:iOS之计算上次日期距离现在多久, 如 xx 小时前、xx 分钟前等

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

天猫淘宝推出跨境供货平台,助力卖家跨境出海:https://www.ikjzd.com/articles/1559386593494417409
跨境电商从业者平均工资出炉,你拖后腿了吗?:https://www.ikjzd.com/articles/15594
封号大屠杀来袭!上万卖家迎来亚马逊暴击:https://www.ikjzd.com/articles/1559424113149435905
全球“订单荒”!制造业遇大寒冬!工厂老板跑路,订单断崖式下跌……:https://www.ikjzd.com/articles/1559430689910382594
海外电子邮件营销的成本是多少?:https://www.ikjzd.com/articles/1559432850874523650
为了宣传,Shopee在巴西整了一部电视剧:https://www.ikjzd.com/articles/1559433306850758658
亚龙湾有什么景点必看 亚龙湾有哪些景点:https://www.vstour.cn/a/408228.html
怀柔国际会议景点 怀柔国家会议中心门票:https://www.vstour.cn/a/408229.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流