处理时间格式的代码处理
2016-01-12 10:30
218 查看
-(NSString *) FormartTime:(NSDate*) compareDate { if( compareDate == nil ) return @""; NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow]; timeInterval = -timeInterval; long temp = timeInterval; NSString *result; if (timeInterval < 60) { if( temp == 0 ) result = @"刚刚"; else result = [NSString stringWithFormat:@"%d秒前",(int)temp]; } else if(( timeInterval/60) <60){ result = [NSString stringWithFormat:@"%d分钟前",(int)temp/60]; } else if(( temp/86400) <30){ NSDateFormatter *date = [[NSDateFormatter alloc] init]; [date setDateFormat:@"dd"]; NSString *str = [date stringFromDate:[NSDate date]]; int nowday = [str intValue]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd"]; NSString *strDate = [dateFormatter stringFromDate:compareDate]; int day = [strDate intValue]; if (nowday-day==0) { //[dateFormatter setDateFormat:@"今天 HH:mm"]; [dateFormatter setDateFormat:@"HH:mm"]; result = [dateFormatter stringFromDate:compareDate]; } else if(nowday-day==1) { [dateFormatter setDateFormat:@"昨天 HH:mm"]; // result = [dateFormatter stringFromDate:compareDate]; result = @"昨天"; } else if( temp < 8 ) { if (temp==1) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"昨天HH:mm"]; NSString *strDate = [dateFormatter stringFromDate:compareDate]; result = strDate; } else if(temp == 2) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"前天HH:mm"]; NSString *strDate = [dateFormatter stringFromDate:compareDate]; result = strDate; } } else { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM-dd HH:mm"]; NSString *strDate = [dateFormatter stringFromDate:compareDate]; result = strDate; } } else {//超过一个月的就直接显示时间了 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *strDate = [dateFormatter stringFromDate:compareDate]; result = strDate; } /* else if((temp = (temp/(3600*24))/30) <12){ result = [NSString stringWithFormat:@"%d个月前",(int)temp]; } else{ temp = temp/12; result = [NSString stringWithFormat:@"%d年前",(int)temp]; } */ return result; }
相关文章推荐
- laravel中遇到的问题
- Python系列之 - Collections系列
- 从0到1 python简易计算器
- Eclipse"启动"问题:Failed to load the JNI shared library jvm.dll
- Java中List与Map初始化的一些写法分享
- Spring中@Autowired注解、@Resource注解的区别
- java 线程、线程池基本应用演示样例代码回想
- Eclipse闪退/打不开/无法启动/一闪而过
- Qt使用教程之创建移动应用程序(三)
- 【第八章】 对ORM的支持 之 8.2 集成Hibernate3 ——跟我学spring3
- Cairo Tutorial for Python Programmers
- PHP加密与解密
- 策略模式(StragetyPattern)学习记录
- 【c/c++】assert()函数
- 解决remove @override annotation(jdk1.5和jdk1.6)
- ASPxGridView 用法整理
- C#6.0新特性
- 深入理解java异常处理机制
- C 和 C++ 的没落
- 堆排序(Python版)