NSDate计算两个时间日期的时差
2016-04-20 16:39
399 查看
小记一笔,免得下次翻阅的时候,又浪费太多时间去验证。
经常需要比对两个时间的差距,当差距7天的时候就给出一定的提示等等什么的。
代码如下:
经常需要比对两个时间的差距,当差距7天的时候就给出一定的提示等等什么的。
代码如下:
- (BOOL)isOutOfDateTime { //updateTime格式如下:2016-03-22 NSDateFormatter *format = [[NSDateFormatter alloc]init]; //根据updateTime的格式,写出对应的日期格式化串 [format setDateFormat:@"yyyy-MM-dd"]; [format setLocale:[NSLocale currentLocale]]; NSDate *currentDate = [format dateFromString:updateTime]; //获取当前的系统时间 NSDate *date = [NSDate date]; //消除8小时的误差。 NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate:date]; //追加8小时 NSDate *localeDate = [date dateByAddingTimeInterval: interval]; currentDate = [currentDate dateByAddingTimeInterval:interval]; //计算时间差间隔 NSTimeInterval timeBetween = [localeDate timeIntervalSinceDate:currentDate]; //根据相差的秒数,看是否大于7天 if (timeBetween > 7 * 24 * 3600) { return YES; } return NO; }
相关文章推荐
- ScrollView中软键盘弹出依然遮住控件
- DataTables错误: requested unknown parameter
- WebService和Webapi的区别
- 正确使用 Volatile 变量
- 基于asp.net+MINIUI的项目----在线学习系统
- 是否需要主动调用Bitmap的recycle方法
- JVM基础(二) 实现自己的ClassLoader
- ZooKeeper 安装部署及hello world
- [置顶] [汇编学习笔记][第六章包含多个段的程序]
- 【python】read_csv+编码问题
- [汇编学习笔记][第六章包含多个段的程序]
- 大中型Web站点解决高并发访问的优化方案
- Android 如何加载大图片
- --force
- spring事务管理
- C++里的继承和多态(下)——单继承、多继承、菱形继承(含虚拟函数的继承)
- 投简历,切忌这三大雷区!
- 腾讯2016校招技术岗面试经历
- iOS APP 发布上架流程
- $Android AlarmManager的用法详解