iOS日期处理
来源:互联网 发布:网络安全技术高峰论坛 编辑:程序博客网 时间:2024/06/06 00:45
//转换创建时间的格式 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; fmt.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"]; NSDate *createdDate = [fmt dateFromString:_created_at]; //NSDate *createdDate = [fmt dateFromString:@"Thu May 19 13:20:20 +0800 2014"]; fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *createdStr = [fmt stringFromDate:createdDate]; createdDate = [fmt dateFromString:createdStr]; NSDate *now = [NSDate date];//2015-05-22 14:16:16 +0000 NSCalendar *calender = [NSCalendar currentCalendar]; //why? NSDateComponents *compareComponents = [calender components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:createdDate toDate:now options:0]; //!!!!!!! //NSLog(@"%@",compareComponents); NSDateComponents *createdComponents = [calender components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:createdDate]; NSDateComponents *nowComponents = [calender components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:now]; BOOL isThisYear = createdComponents.year == nowComponents.year; BOOL isToday = createdComponents.month == nowComponents.month && createdComponents.day == nowComponents.day; BOOL isYesterday = createdComponents.month == nowComponents.month && compareComponents.day == 1; if (isThisYear) { //今年 if (isToday) {//今天 if (fabs([now timeIntervalSinceDate:createdDate]) < 60) {//小于一分钟 return @"刚刚"; } else if (compareComponents.hour == 0) { //小于一个小时 return [NSString stringWithFormat:@"%ld分钟前",compareComponents.minute]; } else { //大于一个小时 return [NSString stringWithFormat:@"%ld小时前",compareComponents.hour]; } } else if (isYesterday) { //昨天 fmt.dateFormat = @"HH:mm:ss"; NSString *str = [fmt stringFromDate:createdDate]; return [NSString stringWithFormat:@"昨天 %@",str]; } else { //今年的其他日子 fmt.dateFormat = @"MM-dd HH:mm:ss"; return [fmt stringFromDate:createdDate]; } } else {//其他年份 return [fmt stringFromDate:createdDate]; }
0 0
- iOS 日期处理 - 解析日期
- ios日期处理
- iOS日期处理
- ios日期处理
- IOS日期处理
- iOS日期处理小结
- iOS日期前后比较 && 日期处理
- iOS日期与时间处理
- iOS 时间与日期处理
- iOS开发中的日期处理 -
- IOS时间与日期处理
- iOS 时间与日期处理
- iOS 时间与日期处理
- iOS开发:日期与时间的处理
- iOS 时间,日期,字符串的处理
- ios开发-处理服务器返回的日期
- iOS NSDateFormatter日期时间格式处理的用法
- iOS中时间/日期处理NSDate的常用接口操作
- zookeeper应用场景!
- 第六篇 毕业感悟
- (LeetCode初探)从A+B开始
- Ubuntu上配置静态ip
- Java面试题32问
- iOS日期处理
- toj 2190 Nuanran's Idol
- 通过ip地址连接virtualbox虚拟机android-x86
- 企业发展的十大专利战略
- CodeBlocks 的使用方法(不定期更新)
- jvm hotspot 虚拟机源代码编译
- VC中调用外部EXE程序方法(转)
- DOM解析XML文件3-SimpleAdapter_自定义布局
- 斐波那契数列——矩阵加速递推