iOS NSDate,NSDateFormatter,NSTimeZone,NSTimeInterval
来源:互联网 发布:手机淘宝支付宝登录 编辑:程序博客网 时间:2024/04/27 18:27
最近开发中做了很多时间处理,总之真的很烦,所以来总结一下。
//NSDate是用来存储时间 //NSDate 的初始化 //初始化一个date对象,获取当前时间和日期。 //注意:1.当前时区是GMT 与北京时间相差8个小时 NSDate *date1 = [NSDate date]; NSLog(@"date1 == %@",date1); //date1 == 2016-11-04 06:16:54 +0000
//NSTimeZone 是有时间差计算的方法的 //我们通过NSTimeZone 计算时间差,就可以得到想要的时区对应的NSDate对象。 NSTimeZone *timeZone = [NSTimeZone systemTimeZone]; NSTimeInterval interval = [timeZone secondsFromGMTForDate:date1]; NSDate *localedate = [date1 dateByAddingTimeInterval:interval]; NSLog(@"localdate == %@",localedate); //localdate == 2016-11-04 14:16:54 +0000
//计算CST GMT 的时间差 NSTimeInterval timeIntervalfromDate1ToLocale = [date1 timeIntervalSinceDate:localedate]; NSLog(@"%f",timeIntervalfromDate1ToLocale); //-28800.000000 //根据结果可以看到 dateWithTimeInterval:sinceDate //TimeInterval 为负值,计算结果是无效的 //所以我觉得存储时间数据还是存储GMT的比较好,因为,方便转换! NSDate *localeDateToGMTDate = [NSDate dateWithTimeInterval:timeIntervalfromDate1ToLocale sinceDate:localedate]; NSDate *localeDateToGMTDate2 = [localedate dateByAddingTimeInterval:timeIntervalfromDate1ToLocale]; NSLog(@"localeDateToGMTDate == %@",localeDateToGMTDate); NSLog(@"localeDateToGMTDate2 == %@",localeDateToGMTDate2); //localeDateToGMTDate == 2016-11-04 06:16:54 +0000 //localeDateToGMTDate2 == 2016-11-04 06:16:54 +0000
//dateFormatter --> 默认是的系统的时区 //dateFormatter 有 locale,timezone,calendar等属性 //注意:用dateFormatter 对date1 进行格式化 会自动到当前时区。 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; //亲测,设置locale 不好用 //dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"]; NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; //改变NSDateFormatter 的时区 还是设置timeZone比较好用 //[dateFormatter setTimeZone:gmtTimeZone]; [dateFormatter setDateFormat:@"dd日H时mm分"]; NSString *date1String = [dateFormatter stringFromDate:date1]; NSLog(@"dateString == %@",date1String); //dateFormatterLocaldateString == 04日22时19分 //如果用CST(北京时区) 打印结果加上系统认为 系统当前时区与GMT的时间差 //结果会多了八个小时 //所以用dateFormatter 时要注意时区问题 NSString *localdateString = [dateFormatter stringFromDate:localedate]; NSLog(@"dateFormatterLocaldateString == %@",localdateString); //dateFormatterLocaldateString == 04日22时19分 /* //dateFormatter 的格式可以指定以下内容 G: 公元时代,例如AD公元 yy: 年的后2位 yyyy: 完整年 MM: 月,显示为1-12 MMM: 月,显示为英文月份简写,如 Jan MMMM: 月,显示为英文月份全称,如 Janualy dd: 日,2位数表示,如02 d: 日,1-2位显示,如 2 EEE: 简写星期几,如Sun EEEE: 全写星期几,如Sunday aa: 上下午,AM/PM H: 时,24小时制,0-23 K:时,12小时制,0-11 m: 分,1-2位 mm: 分,2位 s: 秒,1-2位 ss: 秒,2位 S: 毫秒 */
NSDate *date3 = [NSDate date]; //date3与当前时间的时间差 NSTimeInterval timeIntervalSinceNow = date3.timeIntervalSinceNow; NSTimeInterval timeIntervalSince1970 = date3.timeIntervalSince1970; NSTimeInterval timeIntervalSinceReferenceDate = [date3 timeIntervalSinceReferenceDate]; NSTimeInterval timeIntervalSinceDate = [date3 timeIntervalSinceDate:date1]; //时间的比较 /* - (NSDate *)earlierDate:(NSDate *)anotherDate; - (NSDate *)laterDate:(NSDate *)anotherDate; - (NSComparisonResult)compare:(NSDate *)other; - (BOOL)isEqualToDate:(NSDate *)otherDate; */ //传递一个locale 返回描述date在当前local 描述时间的字符串 //如果你传递一个字典,那么就会使用[NSLocale currentLocale] //如果传递的是nil,那么就会使用和description属性一样的描述(GMT时区). NSDictionary *dic = [[NSDictionary alloc]init]; NSString *descriptionWithLocaleWithDic = [date1 descriptionWithLocale:dic]; NSString *descriptionWithLocaleWithNil = [date1 descriptionWithLocale:nil]; NSString *descriptionWithLocaleWithLocale = [date1 descriptionWithLocale:[NSLocale currentLocale]]; NSLog(@"descriptionWithLocaleWithDic == %@\n descriptionWithLocaleWithNil == %@\n descriptionWithLocaleWithLocale == %@",descriptionWithLocaleWithDic,descriptionWithLocaleWithNil,descriptionWithLocaleWithLocale); //descriptionWithLocaleWithDic == Friday, November 4, 2016 at 2:19:09 PM China Standard Time //descriptionWithLocaleWithNil == 2016-11-04 06:19:09 +0000 //descriptionWithLocaleWithLocale == Friday, November 4, 2016 at 2:19:09 PM China Standard Time
//NStimeZone //本地时区 默认时区 当前时区 都是 GMT+8 //localTimeZone == Local Time Zone (Asia/Shanghai (GMT+8) offset 28800) //defaultTimeZone == Asia/Shanghai (GMT+8) offset 28800 //currentTimeZone == Asia/Shanghai (GMT+8) offset 2880o NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone]; NSTimeZone *currentTimeZone = [NSTimeZone systemTimeZone]; NSLog(@"localTimeZone == %@,defaultTimeZone == %@,currentTimeZone == %@",localTimeZone,defaultTimeZone,currentTimeZone); //localTimeZone == Local Time Zone (Asia/Shanghai (GMT+8) offset 28800),defaultTimeZone == Asia/Shanghai (GMT+8) offset 28800,currentTimeZone == Asia/Shanghai (GMT+8) offset 28800
1 0
- iOS NSDate,NSDateFormatter,NSTimeZone,NSTimeInterval
- NSDate、NSTimeInterval、NSDateFormatter、NSLocale 、NSTimeZone、NSDateComponents详解
- iOS时间处理(NSDate、NSDateComponents、NSCalendar、NSDateFormatter、NSTimeZone)
- NSDate NSDateFormatter NSTimeZone NSDateComponents NSCalendar
- iOS NSTimeInterval 转成 NSDate
- iOS NSTimeInterval 转成 NSDate
- 日期和时间 NSDate NSTimeInterval NSCalendar NSDateComponents NSDateFormatter
- iOS NSDate NSDateFormatter
- Foundation框架下的常用类--NSDate、NSDateFormatter 、NSTimeZone
- 关于NSDate,NSDateFormatter,NSTimeZone的一些通俗理解
- Foundation框架浅析 - 时间相关的类:NSDate、NSTimeZone、NSLocale、NSDateComponents、NSCalendar、NSDateFormatter
- 【精】iOS 日期操作总结:NSDate、NSDateFormatter
- IOS 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)
- NSTimeZone和NSDate
- iOS NSTimeZone
- iOS下时间基础处理(NSDate NSDateFormatter等)
- iOS关于NSDate、NSString、NSDateFormatter设置时间格式等问题
- ios开发--NSDate与NSDateFormatter的相关用法
- Android的ToggleButton实现开关按钮
- ajax,json,php使用实例
- 1
- C++ 类访问控制public/private/protected探讨
- caffe 使能python接口使用draw_net.py绘制网络结构图过程
- iOS NSDate,NSDateFormatter,NSTimeZone,NSTimeInterval
- mfc一个日志库,非常好用,nice
- 网络编程基础(5)-协议概要-TCP的超时重传
- html meta中的viewport
- git学习笔记
- OpenGL 矩阵及变换概念
- (十三)ReactNative--- React.creatClass 和React.component的区别
- Linux基本的架站流程表
- 如何将我的php脚本以守护进程的方式一直运行