iOS根据日期判断是刚刚、几分钟前、几小时前等的代码片段
来源:互联网 发布:linux 系统 死机 日志 编辑:程序博客网 时间:2024/04/30 06:21
- 是NSDate的一个扩展方法:
- - (NSString *)prettyDateWithReference:(NSDate *)reference {
- NSString *suffix = @"ago";
- float different = [reference timeIntervalSinceDate:self];
- if (different < 0) {
- different = -different;
- suffix = @"from now";
- }
- // days = different / (24 * 60 * 60), take the floor value
- float dayDifferent = floor(different / 86400);
- int days = (int)dayDifferent;
- int weeks = (int)ceil(dayDifferent / 7);
- int months = (int)ceil(dayDifferent / 30);
- int years = (int)ceil(dayDifferent / 365);
- // It belongs to today
- if (dayDifferent <= 0) {
- // lower than 60 seconds
- if (different < 60) {
- return @"just now";
- }
- // lower than 120 seconds => one minute and lower than 60 seconds
- if (different < 120) {
- return [NSString stringWithFormat:@"1 minute %@", suffix];
- }
- // lower than 60 minutes
- if (different < 660 * 60) {
- return [NSString stringWithFormat:@"%d minutes %@", (int)floor(different / 60), suffix];
- }
- // lower than 60 * 2 minutes => one hour and lower than 60 minutes
- if (different < 7200) {
- return [NSString stringWithFormat:@"1 hour %@", suffix];
- }
- // lower than one day
- if (different < 86400) {
- return [NSString stringWithFormat:@"%d hours %@", (int)floor(different / 3600), suffix];
- }
- }
- // lower than one week
- else if (days < 7) {
- return [NSString stringWithFormat:@"%d day%@ %@", days, days == 1 ? @"" : @"s", suffix];
- }
- // lager than one week but lower than a month
- else if (weeks < 4) {
- return [NSString stringWithFormat:@"%d week%@ %@", weeks, weeks == 1 ? @"" : @"s", suffix];
- }
- // lager than a month and lower than a year
- else if (months < 12) {
- return [NSString stringWithFormat:@"%d month%@ %@", months, months == 1 ? @"" : @"s", suffix];
- }
- // lager than a year
- else {
- return [NSString stringWithFormat:@"%d year%@ %@", years, years == 1 ? @"" : @"s", suffix];
- }
- return self.description;
- }
0 0
- iOS根据日期判断是刚刚、几分钟前、几小时前等的代码片段
- iOS根据日期判断是刚刚、几分钟前、几小时前等的代码片段
- iOS根据日期判断是刚刚、几分钟前还是几小时前等
- 日期输出为刚刚、几分钟前、几小时前、日期
- 日期转成具体的几分钟前,几小时前等...
- 格式化日期,刚刚,几秒前,几分钟前,几小时前.....
- iOS计算时间,刚刚,几分钟前,几小时前
- NSDate: 处理时间(刚刚、几分钟前、几小时前等)
- 工具类:计算距离现在的时间:刚刚/几分钟前/几小时前/几天前/几个月前/几年前
- JS、jquery实现几分钟前、几小时前、几天前等时间差显示效果的代码实例
- iOS常用于显示几小时前/几天前/几月前/几年前的代码片段
- java实现几分钟前,几小时前,几天前的代码
- js计算时间为刚刚、几分钟前、几小时前、几天前··
- iOS 将时间转成刚刚、几分钟、几小时等
- 时间以几分钟、几小时前、几天前等时间差显示的js函数
- 时间以几分钟、几小时前、几天前等时间差显示的js函数
- 对时间进行格式化,例如刚刚、几秒前、几分钟前、几小时前、昨天、前天
- NSDate 实现时间格式刚刚、几分钟前、几小时前、昨天、MM-dd HH:mm:ss
- 删除链表中重复的结点
- Mysql 5.5多实例部署步骤
- 表格布局—计算器
- 第十、十一周项目1:阅读程序,写出执行结果(1)
- android登陆模块 联网登陆和离线登陆 类似微信离线登陆
- iOS根据日期判断是刚刚、几分钟前、几小时前等的代码片段
- 关于com工程依赖的一些总结
- sigwait函数
- 第十、十一周项目1:阅读程序,写出执行结果(2)
- listview属性动画
- HTML5 -- 网页框架bootstrap的简单使用
- 第十、十一周项目1:阅读程序,写出执行结果(3)
- 第十、十一周项目1:阅读程序,写出执行结果(4)
- python安装pip