开发锦集

距今多久之前

2017-03-27  本文已影响14人  YvanLiu
#pragma mark - 多久之前

+ (NSString *)timeConversionWithOldTime:(NSString *)oldTime NewTime:(NSString *)newTime {
    
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * lastDate = [dateFormatter dateFromString:oldTime];
    NSDate * nowDate = [dateFormatter dateFromString:newTime];
    
    NSTimeInterval time = [nowDate timeIntervalSinceDate:lastDate];
    NSTimeInterval retTime;
    if (time < 3600) {//小于一小时
        retTime = time/60;
       return (retTime<1.0)?@"刚刚":[NSString stringWithFormat:@"%.0f分钟前",retTime];
    } else if (time < 3600*24) {//小于一天
        retTime = time/3600;
        return (retTime < 1)?@"1小时前":[NSString stringWithFormat:@"%.0f小时前",retTime];
    } else if (time < 3600*24*30) {
        retTime = time/(3600*24);
        return [NSString stringWithFormat:@"%.0f天前",retTime];
    } else {
        return @"30天前";
    }
}
上一篇下一篇

猜你喜欢

热点阅读