iOS规范化时间格式,object-C计算指定时间与当前的时间差
2015-08-12 11:25
417 查看
object-c计算指定时间与当前的时间差
头文件(.h):
.m文件:
头文件(.h):
#import <Foundation/Foundation.h> @interface LuDate : NSDate +(NSString *) compareCurrentTime:(NSString*) strDate; @end
.m文件:
/** * 计算指定时间与当前的时间差 * @param compareDate 某一指定时间 * @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前) */ +(NSString *) compareCurrentTime:(NSString*) strDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *compareDate= [dateFormatter dateFromString:strDate]; NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow]; timeInterval = -timeInterval; long temp = 0; NSString *result; if (timeInterval < 60) { result = [NSString stringWithFormat:@"刚刚"]; } else if((temp = timeInterval/60) <60){ result = [NSString stringWithFormat:@"%ld分前",temp]; } else if((temp = temp/60) <24){ result = [NSString stringWithFormat:@"%ld小前",temp]; } else if((temp = temp/24) <30){ result = [NSString stringWithFormat:@"%ld天前",temp]; } else if((temp = temp/30) <12){ result = [NSString stringWithFormat:@"%ld月前",temp]; } else{ temp = temp/12; result = [NSString stringWithFormat:@"%ld年前",temp]; } return result; } @end
相关文章推荐
- 我的iOS开发之Objective-C第2天之面向对象特性之封装
- 关于Object类中equals方法的重写
- Objective-C Modernization Tool
- Objective-C——判断对象等同性
- 【Objective-C】构造方法description等
- JSONObject中取String 值的几种方法和对比
- JavaSE----API之常用类(Object、Scanner、String)
- Objective-C——判断对象等同性
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- IOS基础:深入理解Objective-c中@class的含义
- 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
- java Object类占用内存大小计算
- Objective-C——关联对象
- jQuery object & dom object
- 黑马程序员——Java基础---常用类--Object类,Scanner类,String类,StringBuffer类,StringBuilder类
- Objective-C中的instancetype和id关键字
- Objective-C Runtime (一)
- 我的iOS开发之Objective-C第8天之延展和协议以及深浅复制
- Objective C ARC 使用及原理
- Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)