iOS NSDate的常用API

打印 上一主题 下一主题

主题 974|帖子 974|积分 2922

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x


目次

一、创建日期
1.获取当前时间
2.当前时间指定秒数之后/前的时间
3.指定日期之后/后的时间
4.2001年之后/前指定秒数的时间
5.1970年之后/后指定秒数的时间
二、初始化日期
1.init
2.时间间指定秒数的时间
3.指定时间指定秒数之前/后的时间
4.2001年指定秒数之后/前的时间
三、获取时间边界
1.distantFuture
2.distantPast
四、时间比较
1.比较日期是否相等
2.比较日期是否较早
3.比较日期是否较晚
4.带回调的日期比较的方法
五、获取时间间隔
1.与指定日期的时间间隔
2.间隔如今的时间间隔
3.间隔2001年的时间间隔
4.间隔1970年的时间间隔 
六、时间的运算
总结


前言
     

        本文整理了一下NSDate的API,以便必要大概忘记的时候查询下用法,文章末端贴出了开发过长中常用的NSDate的拓展方法。
        系统默认NSDate的默认时间为UTC时间。
一、创建日期

1.获取当前时间

   + (instancetype)date;
          这里返回的当前的时间(UTC时间),下面的示例展示了获取当前时间的方法。        
  1. NSDate * todayDate = [NSDate date];
  2. NSLog(@"当前时间:%@",todayDate);
复制代码
2.当前时间指定秒数之后/前的时间

   + (instancetype)dateWithTimeIntervalSinceNowNSTimeInterval)secs;
          这里返回的时候当前时间之后多少秒的时间。使用负数表现之前多少秒的时间。
  1. NSDate * todayDate = [NSDate date];
  2. NSLog(@"当前时间:%@",todayDate);NSDate * afterTenSeconds = [NSDate dateWithTimeIntervalSinceNow:10];NSLog(@"10秒钟之后的时间:%@",afterTenSeconds);NSDate * beforeTenSeconds = [NSDate dateWithTimeIntervalSinceNow:-10];NSLog(@"10秒钟之前的时间:%@",beforeTenSeconds);
复制代码
3.指定日期之后/后的时间

   + (instancetype)dateWithTimeIntervalNSTimeInterval)secsToBeAdded sinceDateNSDate *)date;
          指定日期多少秒之后的时间,负数表现指定日期多少秒之前的时间。
  1. NSDate * todayDate = [NSDate date];   
  2. NSLog(@"当前时间:%@",todayDate);     
  3. NSDate * afterTenSecondDate = [NSDate dateWithTimeInterval:10 sinceDate:todayDate];        
  4. NSDate * beginTenSecondDate = [NSDate dateWithTimeInterval:-10 sinceDate:todayDate];   
  5. NSLog(@"10秒钟之后的时间:%@",afterTenSecondDate);
  6. NSLog(@"10秒钟之后的时间:%@",beginTenSecondDate);
复制代码
4.2001年之后/前指定秒数的时间

   + (instancetype)dateWithTimeIntervalSinceReferenceDateNSTimeInterval)ti;
          返回从 2001 年 1 月 1 日 00:00:00 UTC 开始的给定秒数之后的时间,负数表现给定秒数之前的时间。
  1. NSDate * todayDate = [NSDate date];
  2. NSLog(@"当前时间:%@",todayDate);NSDate * tenSecondsAfter2001 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];  NSLog(@"2001-1-1 00:00:00 之后10秒的时间当前时间:%@",tenSecondsAfter2001);
复制代码
5.1970年之后/后指定秒数的时间

   + (instancetype)dateWithTimeIntervalSince1970NSTimeInterval)secs;
          返回从 1970 年 1 月 1 日 00:00:00 UTC 开始的给定秒数之后的时间,负数表现给定秒数之前的时间。
  1. NSDate * tenSecondsAfter1970 = [NSDate dateWithTimeIntervalSince1970:10];
  2. NSLog(@"2001-1-1 00:00:00 之后10秒的时间当前时间:%@",tenSecondsAfter1970);
复制代码
二、初始化日期

1.init

   - (instancetype)init;
          这里返回的也是当前的时间
  1. NSDate * nowDate = [[NSDate alloc]init];
  2. NSLog(@"当前时间:%@",nowDate);
复制代码
2.时间间指定秒数的时间

   - (instancetype)initWithTimeIntervalSinceNowNSTimeInterval)secs;
  1. NSDate * nowDate = [[NSDate alloc]init];   
  2. NSLog(@"当前时间:%@",nowDate);
  3. NSDate * afterTenSecondsDate = [[NSDate alloc]initWithTimeIntervalSinceNow:10];
  4. NSLog(@"10秒钟之后的时间:%@",afterTenSecondsDate);
复制代码
3.指定时间指定秒数之前/后的时间

   - (instancetype)initWithTimeIntervalNSTimeInterval)secsToBeAdded sinceDateNSDate *)date;
  1. NSDate * nowDate = [[NSDate alloc]init];        
  2. NSLog(@"当前时间:%@",nowDate);
  3. NSDate * afterTenSecondsDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:nowDate];        
  4. NSLog(@"10秒钟之后的时间:%@",afterTenSecondsDate);
复制代码
4.2001年指定秒数之后/前的时间

   - (instancetype)initWithTimeIntervalSinceReferenceDateNSTimeInterval)ti;
  1. NSDate * nowDate = [[NSDate alloc]init];        
  2. NSLog(@"当前时间:%@",nowDate);        
  3. NSDate * afterTenSecondsFrom2001 = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:10];        
  4. NSLog(@"2001年10秒钟之后的时间:%@",afterTenSecondsFrom2001);
复制代码
5.1970年指定秒数之后/前的时间
   - (instancetype)initWithTimeIntervalSince1970NSTimeInterval)secs;
  1. NSDate * nowDate = [[NSDate alloc]init];        
  2. NSLog(@"当前时间:%@",nowDate);        
  3. NSDate * afterTenSecondsFrom1970 = [[NSDate alloc]initWithTimeIntervalSince1970:10];        
  4. NSLog(@"1970年10秒钟之后的时间:%@",afterTenSecondsFrom1970);
复制代码
三、获取时间边界

1.distantFuture

2.distantPast

四、时间比较

1.比较日期是否相等

   - (BOOL)isEqualToDate:(NSDate *)otherDate;
          比较A日期是否和B日期是否相同。相同返回YES,不同返回NO。
  1. NSDate * nowDate = [NSDate now];
  2. NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];
  3. if ([nowDate isEqualToDate:nowDateAfter10Seconds]) {
  4.     NSLog(@"nowDate == nowDateAfter10Seconds");
  5. } else {
  6.     NSLog(@"nowDate != nowDateAfter10Seconds");
  7. }
  8. if ([[NSDate now] isEqualToDate:[[NSDate alloc]init]]) {
  9.     NSLog(@"equal");
  10. }
复制代码
2.比较日期是否较早

   - (NSDate *)earlierDate:(NSDate *)anotherDate;
          比较A日期是否早于B日期,是的话返回YES,否则返回NO。
  1. NSDate * nowDate = [NSDate now];
  2. NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];
  3. if ([nowDate isEarlierThanOrEqualTo:nowDateAfter10Seconds]) {
  4.      NSLog(@"nowDate isEarlierThanOrEqualTo nowDateAfter10Seconds");
  5. } else {
  6.     NSLog(@"nowDate isNotEarlierThanOrEqualTo nowDateAfter10Seconds");
  7. }
复制代码
3.比较日期是否较晚

   - (NSDate *)laterDate:(NSDate *)anotherDate;
          比较A日期是否晚与B日期和上面的返回结果刚好相反。
  1.    
  2. NSDate * nowDate = [NSDate now];
  3. NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];   
  4. if ([nowDate isLaterThan:nowDateAfter10Seconds]) {
  5.    NSLog(@"nowDate isLaterThan nowDateAfter10Seconds");   
  6. } else {
  7.     NSLog(@"nowDate isNotLaterThan nowDateAfter10Seconds");
  8. }
复制代码
4.带回调的日期比较的方法

   - (NSComparisonResult)compare:(NSDate *)other;
          这里这个方法的返回值是NSComparisonResult类型的枚举,当两个日期完全相同的时候返回NSOrderedSame。  

  • A < B     返回 NSOrderedAscending
  • A > B     返回 NSOrderedSame
  • A == B   返回 NSOrderedDescending
  1. NSDate * nowDate = [NSDate now];=   
  2. NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];  
  3. NSComparisonResult  comparisonResult = [nowDate compare:nowDateAfter10Seconds];
  4. NSLog(@"比较结果:%ld",comparisonResult );
复制代码
五、获取时间间隔

1.与指定日期的时间间隔

   - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;        
          返回和指定日期的时间间隔,单位是秒。
  1. NSDate * nowDate = [NSDate now];
  2. NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];
  3. NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:nowDateAfter10Seconds];
  4. NSLog(@"nowDate和nowDateAfter10Seconds间隔%lf秒",timeInterval);
复制代码
2.间隔如今的时间间隔

   @property (readonly) NSTimeInterval timeIntervalSinceNow;
          返回指定日期间隔如今的时间间隔。
  1. NSDate * nowDate = [NSDate now];   
  2. NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];
  3. NSTimeInterval timeInterval = nowDateAfter10Seconds.timeIntervalSinceNow;  
  4. NSLog(@"timeInterval:%lf秒",timeInterval);
复制代码
3.间隔2001年的时间间隔

   @property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;        
          返回指定日期间隔2001年1月1日的时间间距,单位是秒。
  1. NSDate * nowDate = [NSDate now];   
  2. NSTimeInterval timeInterval = nowDate.timeIntervalSinceReferenceDate;   
  3. NSLog(@"timeInterval:%lf秒",timeInterval);
复制代码
4.间隔1970年的时间间隔 

   @property (readonly) NSTimeInterval timeIntervalSince1970; 
          返回指定日期间隔1970年1月1日的时间间距,单位是秒。
  1. NSDate * nowDate = [NSDate now];   
  2. NSTimeInterval timeInterval = nowDate.timeIntervalSince1970;   
  3. NSLog(@"timeInterval:%lf秒",timeInterval);
复制代码
六、时间的运算

   - (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;   
          返回指定秒数之后的时间。
  1. NSDate * nowDate = [NSDate now];
  2. NSDate * nowDateAfter = [nowDate dateByAddingTimeInterval:24 * 60 * 60];
  3. NSLog(@"nowDateAfter:%@",nowDateAfter);
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

冬雨财经

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表