获取发布时间距离当前时间的时间
2015-12-31 09:54
309 查看
- (NSDate *)dateFromString:(NSString *)dateString{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *destDate= [dateFormatter dateFromString:dateString]; return destDate; } - (NSString *)prettyDateWithReference:(NSDate *)reference { NSString *suffix = @"ago"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";//日期格式 // formatter.timeZone = [NSTimeZone systemTimeZone];//给定系统所在的时区 NSString *dateS = [formatter stringFromDate:[NSDate date]]; NSDate *date = [self dateFromString:dateS]; NSLog(@"%@",date); NSLog(@"%@",dateS); float different = [reference timeIntervalSinceDate:date]; if (different < 0) { different = -different; suffix = @"发布"; } // 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 @"刚才"; } // lower than 120 seconds => one minute and lower than 60 seconds if (different < 120) { return [NSString stringWithFormat:@"1分钟前 %@", suffix]; } // lower than 60 minutes if (different < 660 * 60) { return [NSString stringWithFormat:@"%d 分钟前 %@", (int)floor(different / 60), suffix]; } // lower than 60 * 2 minutes => one hour and lower than 60 minutes if (different < 7200) { return [NSString stringWithFormat:@"1小时前 %@", suffix]; } // lower than one day if (different < 86400) { return [NSString stringWithFormat:@"%d 小时前 %@", (int)floor(different / 3600), suffix]; } } // lower than one week else if (days < 7) { return [NSString stringWithFormat:@"%d 天%@ %@", days, days == 1 ? @"" : @"s", suffix]; } // lager than one week but lower than a month else if (weeks < 4) { return [NSString stringWithFormat:@"%d 星期%@ %@", weeks, weeks == 1 ? @"" : @"s", suffix]; } // lager than a month and lower than a year else if (months < 12) { return [NSString stringWithFormat:@"%d 月%@ %@", months, months == 1 ? @"" : @"s", suffix]; } // lager than a year else { return [NSString stringWithFormat:@"%d 年%@ %@", years, years == 1 ? @"" : @"s", suffix]; } return self.description; }
相关文章推荐
- 读书笔记:Java并发实战 第13章 显式锁
- Windows Sqlserver Automatic Log Audit Via C/C++
- 怎样实现UIView的旋转
- Android 获取网络状态
- DOM事件探秘之一
- IOS 在iPhone上实现简单Http服务
- crontab的问题总结
- Nodejs源码解析之util
- Maven仓库管理之Nexus
- Centos 下Storm集群的搭建及Java客户端环境
- 震屏动画
- 友盟统计的那些事
- json(http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html)
- C#进行MapX二次开发之图层操作
- UWP x:bind
- qtp 基础编程小技巧(三)
- 查询表的基本信息
- 我的网站
- 11.主题结构
- 观察者模式 - Observer