最近开发中做了很多时间处理,总之真的很烦,所以来总结一下。

    //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-12MMM: 月,显示为英文月份简写,如 JanMMMM: 月,显示为英文月份全称,如 Janualydd: 日,2位数表示,如02d: 日,1-2位显示,如 2EEE: 简写星期几,如SunEEEE: 全写星期几,如Sundayaa: 上下午,AM/PMH: 时,24小时制,0-23K:时,12小时制,0-11m: 分,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 2880oNSTimeZone *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

iOS NSDate,NSDateFormatter,NSTimeZone,NSTimeInterval相关推荐

  1. iOS NSDate与NSTimeInterval之间的互转

    1.获取当前时间 - (NSString *)currentDateStr{NSDate *currentDate = [NSDate date];//获取当前时间,日期NSDate *pastHal ...

  2. 【编程技巧】NSDate,NSDateFormatter,NSTimeInterval

    //获取日期 todaysDate=[NSDate date]; //显示日期和时间 dateFormat = [[NSDateFormatter alloc] init];//NSDate没有自己的 ...

  3. iOS客户端NSDateFormatter那些坑

    NSDateFormatter 会收到用户偏好设置的影响,所以有一些坑: 时区校验 有时候,我们需要把时间字符串转换为long类型的时间戳.比如下面例子: // 将"2016-02-06 0 ...

  4. iOS NSDate等时间类的使用

    一.NSDate NSDate对象用来表示一个具体的时间点. NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate存储的是GMT时间,使用的时候会根据 ...

  5. iOS Objective-C NSDateFormatter的一些使用参考

    NSDateFormatter对象的作用,就是把NSDate对象转换为字符串,以及将字符串转换成NSDate. 1. 利用NSDateFormatterStyle枚举值将NSDate对象转换为字符串 ...

  6. ios NSDate释义

    主要是对ios平台NSDate时间类中的方法的用法解释,以备不时之需: NSDate用来表示公历的GMT时间(格林威治时间). 有下面几种初始化方法: 1. - (id)init 默认初始化,返回当前 ...

  7. 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone) -- IOS(实例)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...

  8. iOS NSTimeInterval 转成 NSDate

    可以把时间戳转化成NSDate 的格式 NSTimeInterval interval=1397036582;       NSDate *date=[NSDate dateWithTimeInter ...

  9. 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...

最新文章

  1. Python制作抽奖系统
  2. 深度学习难分样本挖掘(Hard Mining)
  3. 实现计算机界“大满贯”,芮勇博士再获技术成就大奖
  4. 脑机接口拼写器是否真的安全?华中科技大学研究团队对此做了相关研究
  5. 如何用ASP.NET加密Cookie数据过程分析
  6. 安卓手机 python控制_PyAndroidControl:使用python脚本控制你的安卓设备
  7. springMvc 传子 bean 中有bean
  8. ECS入门之Hello World
  9. fscanf读出来的数字不正确_听了那么多数字英文儿歌,这位牛妈的做法却最能引导孩子输出!...
  10. oracle10g连接自动断开,报ORA-03135错误
  11. 批处理 bat for 详解
  12. python中round(x、2)是什么意思_python中round函数具体使用详解
  13. Excel函数(4)日期、文本函数
  14. 商业智能BI的特点及发展
  15. 在Flutter中创建有意思的滚动效果 - Sliver系列
  16. 51单片机使用PWM调速
  17. NIPS2019:旷视提出DetNAS:首个搜索物体检测Backbone的方法
  18. WireShark 不能正常解析 Radius 包,提示 Malformed Packet .
  19. 高数量类别特征(high-cardinality categorical attributes)的预处理方法
  20. 为何泽塔云GPU云能做到“云端的图形工作站,本地一致的使用体验”?

热门文章

  1. Mac 修改文件夹权限
  2. 光盘文件系统-ISO9660(CDFS)、UDF
  3. 与百年经典奢侈品牌万宝龙跨界合作,“劲黑”系列主题下午茶来了!
  4. 大学计算机专业学科要求,大学各专业选科要求 高校选考科目要求
  5. 北大计算机技术报录比,北大报录比超过100%的专业一览
  6. ctfshow web入门 反序列化 前篇 254-266
  7. 全球关注 这个夏天世界劲刮“青奥风”
  8. SDN 软件定义网络
  9. [小说]魔王冢(4)疑窦初生
  10. 封装和继承作业(java)(二)