iPhone:将日期字符串转换为相对时间戳

iPhone:将日期字符串转换为相对时间戳,第1张

概述我有一个时间戳作为字符串,如: Thu, 21 May 09 19:10:09 -0700 我想将它转换为相对时间戳像’20分钟前’或’3天前’。 使用Objective-C for iPhone的最好的方法是什么? -(NSString *)dateDiff:(NSString *)origDate { NSDateFormatter *df = [[NSDateFormatter al 我有一个时间戳作为字符串,如:

Thu,21 May 09 19:10:09 -0700

我想将它转换为相对时间戳像’20分钟前’或’3天前’。

使用Objective-C for iPhone的最好的方法是什么?

解决方法
-(Nsstring *)dateDiff:(Nsstring *)origDate {    NSDateFormatter *df = [[NSDateFormatter alloc] init];    [df setFormatterBehavior:NSDateFormatterBehavior10_4];    [df setDateFormat:@"EEE,dd MMM yy HH:mm:ss VVVV"];    NSDate *convertedDate = [df dateFromString:origDate];    [df release];    NSDate *todayDate = [NSDate date];    double ti = [convertedDate timeIntervalSinceDate:todayDate];    ti = ti * -1;    if(ti < 1) {        return @"never";    } else  if (ti < 60) {        return @"less than a minute ago";    } else if (ti < 3600) {        int diff = round(ti / 60);        return [Nsstring stringWithFormat:@"%d minutes ago",diff];    } else if (ti < 86400) {        int diff = round(ti / 60 / 60);        return[Nsstring stringWithFormat:@"%d hours ago",diff];    } else if (ti < 2629743) {        int diff = round(ti / 60 / 60 / 24);        return[Nsstring stringWithFormat:@"%d days ago",diff];    } else {        return @"never";    }   }
总结

以上是内存溢出为你收集整理的iPhone:将日期字符串转换为相对时间戳全部内容,希望文章能够帮你解决iPhone:将日期字符串转换为相对时间戳所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存