将时间戳转化为多长时间前

只接写代码

- (NSString*)distanceNowTime{

    double nowTime = [[NSDate date] timeIntervalSince1970];

    NSString *nowStr = [NSString stringWithFormat:@"%.lf",nowTime];

    double nowTimebb = [nowStr doubleValue];

    double time = [self doubleValue];//////注意这里的self是后台反回来的时间戳字符串


    if (time > 999999999999) {

        time = time/1000;

    }

    NSLog(@"==%.lf----%.lf",time,nowTimebb);

    double secondes = nowTime - time;

    NSString *result = @"";

    result = [NSString stringWithFormat:@"%.f秒前",secondes];

    if (secondes>60.0) {

        result = [NSString stringWithFormat:@"%.f分钟前",secondes/60];

    }

    if (secondes>60.0*60.0) {

        result = [NSString stringWithFormat:@"%.f小时前",secondes/(60*60)];

    }

    if (secondes>60.0*60.0*24.0) {

        result = [NSString stringWithFormat:@"%.f天前",secondes/(60*60*24)];

    }

    if (secondes>60.0*60.0*24.0*30.0) {

        result = [NSString stringWithFormat:@"%.f个月前",secondes/(60*60*24*30)];

    }

    if (secondes>60.0*60.0*24.0*365.0) {

        result = [NSString stringWithFormat:@"%.f年前",secondes/(60*60*24*365)];

    }

    return result;

}


这个方法一般写在NSString的扩展或类别中。

最后又什么不足之处还请指正,

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容