time difference for iOS

於和志
2023-12-01
#define DAY_SECONDES 86400
- (NSString *)dateTimeDifference:(NSString *)dateString{
    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
    [dateF setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateOld = [dateF dateFromString:dateString];
    NSTimeInterval time = [dateOld timeIntervalSinceNow];

    NSTimeInterval timeABS = fabs(time);
    float day = floor(timeABS/DAY_SECONDES);
    int hour = fmod(timeABS, DAY_SECONDES)/3600;
    int minute = ceil(fmod(timeABS, 3600)/60.0);

    if (day >30.0) {
        NSRange range = [dateString rangeOfString:@" "];
        return [dateString substringToIndex:range.location];
    }else if (day > 0.0){
        return [NSString stringWithFormat:@"%.0f天前",day];
    }else if (hour > 0){
        return [NSString stringWithFormat:@"%d小时前",hour];
    }else{
        return [NSString stringWithFormat:@"%d分钟前",minute];
    }
}

 类似资料:

相关阅读

相关文章

相关问答