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
原创粉丝点击