Objective-C学习之NSDate简单使用说明

移动开发 iOS
XCode4.6.3,OS X SDK 10.8; NSDate日期操作,获取当前日期,日期比较,日期格式,日期推算,时差解决办法等 。

object-c基础语法NSDate, 主要学习NSDate的设置、获取当前时间、当前时间加减秒后的时间、日期比较、日期转换成NSString等

XCode4.6.3,OS X SDK 10.8; NSDate日期操作,获取当前日期,日期比较,日期格式,日期推算,时差解决办法等 。

 

 

一、NSDate初始化

  1. // 获取当前日期   
  2.  
  3.         NSDate *date = [NSDate date];   
  4.  
  5.              
  6.  
  7.          // 打印结果: 当前时间 date = 2013-08-16 09:00:04 +0000   
  8.  
  9.          NSLog(@"当前时间 date = %@",date);   
  10.  
  11.              
  12.  
  13.          // 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可   
  14.  
  15.          date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];   
  16.  
  17.           
  18.  
  19.         //打印结果:当前时间 往后60s的时间date = 2013-08-16 09:01:04 +0000   
  20.  
  21.         NSLog(@"当前时间 往后60s的时间date = %@",date);  

PS:测试时时间是下午5点,但是得到的当前时间却是上午9点,相差了8小时,是时区的问题

解决办法:

  1. NSTimeZone *zone = [NSTimeZone systemTimeZone];   
  2.  
  3.              
  4.  
  5.          NSInteger interval = [zone secondsFromGMTForDate: date];   
  6.  
  7.             
  8.  
  9.         NSDate *localDate = [date  dateByAddingTimeInterval: interval];   
  10.  
  11.              
  12.  
  13.         // 打印结果 正确当前时间 localDate = 2013-08-16 17:01:04 +0000   
  14.  
  15.         NSLog(@"正确当前时间 localDate = %@",localDate);  

二、NSDate与NSString的转换

  1. /*---- NSDate与NSString----*/  
  2.  
  3.          NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];   
  4.  
  5.              
  6.  
  7.          // 设置日期格式   
  8.  
  9.          [dateFormatter setDateFormat:@"年月日 YYYY/mm/dd 时间 hh:mm:ss"];   
  10.  
  11.             
  12.  
  13.          NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];   
  14.  
  15.              
  16.  
  17.         // 打印结果:dateString = 年月日 2013/10/16 时间 05:15:43   
  18.  
  19.          NSLog(@"dateString = %@",dateString);   
  20.  
  21.              
  22.  
  23.              
  24.  
  25.          // 设置日期格式   
  26.  
  27.          [dateFormatter setDateFormat:@"YYYY-MM-dd"];   
  28.  
  29.             
  30.  
  31.          NSString *year = [dateFormatter stringFromDate:[NSDate date]];   
  32.  
  33.              
  34.  
  35.          // 打印结果:年月日 year = 2013-08-16   
  36.  
  37.          NSLog(@"年月日 year = %@",year);   
  38.  
  39.              
  40.  
  41.          // 设置时间格式   
  42.  
  43.          [dateFormatter setDateFormat:@"hh:mm:ss"];   
  44.  
  45.              
  46.  
  47.          NSString *time = [dateFormatter stringFromDate:[NSDate date]];   
  48.  
  49.              
  50.  
  51.          // 打印结果:时间 time = 05:15:43   
  52.  
  53.         NSLog(@"时间 time = %@",time);  

三、日期的比较

  1. /*----日期时间的比较----*/  
  2.  
  3.          // 当前时间   
  4.  
  5.          NSDate *currentDate = [NSDate date];   
  6.  
  7.              
  8.  
  9.          // 比当前时间晚一个小时的时间   
  10.  
  11.          NSDate *laterDate = [[NSDate alloc] initWithTimeInterval:60*60 sinceDate:[NSDate date]];   
  12.  
  13.              
  14.  
  15.          // 比当前时间早一个小时的时间   
  16.  
  17.          NSDate *earlierDate = [[NSDate alloc] initWithTimeInterval:-60*60 sinceDate:[NSDate date]];   
  18.  
  19.             
  20.  
  21.          // 比较哪个时间迟   
  22.  
  23.          if ([currentDate laterDate:laterDate]) {   
  24.  
  25.              // 打印结果:current-2013-08-16 09:25:54 +0000比later-2013-08-16 10:25:54 +0000晚   
  26.  
  27.              NSLog(@"current-%@比later-%@晚",currentDate,laterDate);   
  28.  
  29.          }   
  30.  
  31.              
  32.  
  33.          // 比较哪个时间早   
  34.  
  35.          if ([currentDate earlierDate:earlierDate]) {   
  36.  
  37.              // 打印结果:current-2013-08-16 09:25:54 +0000 比 earlier-2013-08-16 08:25:54 +0000    
  38.  
  39.              NSLog(@"current-%@ 比 earlier-%@ 早",currentDate,earlierDate);   
  40.  
  41.          }   
  42.  
  43.              
  44.  
  45.          if ([currentDate compare:earlierDate]==NSOrderedDescending) {   
  46.  
  47.              // 打印结果   
  48.  
  49.              NSLog(@"current 晚");   
  50.  
  51.          }   
  52.  
  53.          if ([currentDate compare:currentDate]==NSOrderedSame) {   
  54.  
  55.              // 打印结果   
  56.  
  57.              NSLog(@"时间相等");   
  58.  
  59.          }   
  60.  
  61.          if ([currentDate compare:laterDate]==NSOrderedAscending) {   
  62.  
  63.              // 打印结果   
  64.  
  65.              NSLog(@"current 早");   
  66.  
  67.         }  
责任编辑:张叶青 来源: eoe Android开发者社区
相关推荐

2014-06-25 14:02:59

Objective-CKVO

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2011-08-22 15:31:35

Objective-C协议

2011-07-25 10:30:41

Objective-C Xcode 重构

2011-07-25 11:02:29

Objective-C Xcode 标签

2011-07-25 10:14:13

Objective-C Xcode

2011-07-28 15:11:23

iOS Objective-

2011-08-04 14:58:37

Objective-C Cocoa NSString

2013-08-21 14:57:42

objective-c问题

2011-05-11 14:06:49

Objective-C

2011-07-19 17:24:31

Objective-C 对象

2011-08-04 10:38:17

Objective-C 预处理程序

2013-05-02 10:51:17

iOS开发Objective-C@property

2013-07-24 19:19:03

Objective-CiOS开发动态特性之protoc

2011-08-05 14:03:39

Objective-C 对象 模板

2011-07-25 10:03:06

Objective-C 委托

2011-07-27 16:55:12

Objective-c 闭包

2011-08-04 13:43:30

Objective-C 私有变量

2011-08-15 17:47:13

Objective-CisMemberOfC

2011-08-03 16:22:05

Objective-C CodeBlocks
点赞
收藏

51CTO技术栈公众号