iOS中比较时间的方法
来源:互联网 发布:淘宝商城网上购物 编辑:程序博客网 时间:2024/06/15 00:31
在iOS中有时需要算出时间间隔,然后根据不同的结果做不同的操作, 像微博、论坛应用等等:
#define knewsTimeFormat @"yyyy-MM-dd HH:mm:ss" //你要传过来日期的格式#define kLocaleIdentifier @"en_US" // 发布时间- (NSString *)newsTime:(NSString *)newsTimes{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = knewsTimeFormat; formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:kLocaleIdentifier]; NSDate *date = [formatter dateFromString:newsTimes]; NSDate *now = [NSDate date]; // 比较帖子发布时间和当前时间 NSTimeInterval interval = [now timeIntervalSinceDate:date]; NSString *format; if (interval <= 60) { format = @"刚刚"; } else if(interval <= 60*60){ format = [NSString stringWithFormat:@"发布于前%.f分钟", interval/60]; } else if(interval <= 60*60*24){ format = [NSString stringWithFormat:@"发布于前%.f小时", interval/3600]; } else if (interval <= 60*60*24*7){ format = [NSString stringWithFormat:@"发布于前%d天", (int)interval/(60*60*24)]; } else if (interval > 60*60*24*7 & interval <= 60*60*24*30 ){ format = [NSString stringWithFormat:@"发布于前%d周", (int)interval/(60*60*24*7)]; }else if(interval > 60*60*24*30 ){ format = [NSString stringWithFormat:@"发布于前%d月", (int)interval/(60*60*24*30)]; } formatter.dateFormat = format; return [formatter stringFromDate:date];}
0 0
- iOS中比较时间的方法
- MySql中实现时间比较的方法
- C#中时间大小的比较方法
- sql中时间的比较方法
- iOS中APP版本号的比较方法
- Struts2中在页面中比较时间大小的方法
- 比较时间的三中方法(转)
- js中两个时间值的比较方法
- 关于ASP.net中时间比较的方法 DateTime.Compare()
- Mysql中时间加减和比较大小的方法
- Mysql中时间加减和比较大小的方法
- Java时间比较的方法
- javascript 中时间的比较
- JAVA中时间的比较
- js中时间的比较
- SHELL中时间的比较
- java中时间的比较
- java中时间的比较
- android 状态栏(StatusBar)
- Android媒体扫描代码分析
- JAVA配置
- 论文:Recommendation Based on Contextual Opinions 总结
- android.content.ActivityNotFoundException 出现原因分析
- iOS中比较时间的方法
- 安卓调试注意
- 通过Xfire开发web service接口和客户端调用web service接口接口方法
- Android通过修改Framework隐藏系统状态栏
- 1053. Path of Equal Weight
- Sublime Text 3 快捷键汇总
- android值得珍藏的6个开源框架技术
- Android使用HttpURLConnection发送Get请求
- Java实现敏感词过滤