冬雨财经 发表于 2024-7-10 19:52:17

iOS NSDate的常用API



目次

一、创建日期
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年的时间间隔 
六、时间的运算
总结

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

1.获取当前时间

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

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

   + (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
        指定日期多少秒之后的时间,负数表现指定日期多少秒之前的时间。
NSDate * todayDate = ;   
NSLog(@"当前时间:%@",todayDate);   
NSDate * afterTenSecondDate = ;      
NSDate * beginTenSecondDate = ;   
NSLog(@"10秒钟之后的时间:%@",afterTenSecondDate);
NSLog(@"10秒钟之后的时间:%@",beginTenSecondDate); 4.2001年之后/前指定秒数的时间

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

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

1.init

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

   - (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
NSDate * nowDate = [init];   
NSLog(@"当前时间:%@",nowDate);
NSDate * afterTenSecondsDate = [initWithTimeIntervalSinceNow:10];
NSLog(@"10秒钟之后的时间:%@",afterTenSecondsDate); 3.指定时间指定秒数之前/后的时间

   - (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
NSDate * nowDate = [init];      
NSLog(@"当前时间:%@",nowDate);
NSDate * afterTenSecondsDate = [initWithTimeInterval:10 sinceDate:nowDate];      
NSLog(@"10秒钟之后的时间:%@",afterTenSecondsDate); 4.2001年指定秒数之后/前的时间

   - (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
NSDate * nowDate = [init];      
NSLog(@"当前时间:%@",nowDate);      
NSDate * afterTenSecondsFrom2001 = [initWithTimeIntervalSinceReferenceDate:10];      
NSLog(@"2001年10秒钟之后的时间:%@",afterTenSecondsFrom2001); 5.1970年指定秒数之后/前的时间
   - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
NSDate * nowDate = [init];      
NSLog(@"当前时间:%@",nowDate);      
NSDate * afterTenSecondsFrom1970 = [initWithTimeIntervalSince1970:10];      
NSLog(@"1970年10秒钟之后的时间:%@",afterTenSecondsFrom1970); 三、获取时间边界

1.distantFuture

2.distantPast

四、时间比较

1.比较日期是否相等

   - (BOOL)isEqualToDate:(NSDate *)otherDate;
        比较A日期是否和B日期是否相同。相同返回YES,不同返回NO。
NSDate * nowDate = ;
NSDate * nowDateAfter10Seconds = ;
if () {
    NSLog(@"nowDate == nowDateAfter10Seconds");
} else {
    NSLog(@"nowDate != nowDateAfter10Seconds");
}
if ([ isEqualToDate:[init]]) {
    NSLog(@"equal");
} 2.比较日期是否较早

   - (NSDate *)earlierDate:(NSDate *)anotherDate;
        比较A日期是否早于B日期,是的话返回YES,否则返回NO。
NSDate * nowDate = ;
NSDate * nowDateAfter10Seconds = ;
if () {
   NSLog(@"nowDate isEarlierThanOrEqualTo nowDateAfter10Seconds");
} else {
    NSLog(@"nowDate isNotEarlierThanOrEqualTo nowDateAfter10Seconds");
}
3.比较日期是否较晚

   - (NSDate *)laterDate:(NSDate *)anotherDate;
        比较A日期是否晚与B日期和上面的返回结果刚好相反。
   
NSDate * nowDate = ;
NSDate * nowDateAfter10Seconds = ;   
if () {
   NSLog(@"nowDate isLaterThan nowDateAfter10Seconds");   
} else {
    NSLog(@"nowDate isNotLaterThan nowDateAfter10Seconds");
} 4.带回调的日期比较的方法

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

[*]A < B     返回 NSOrderedAscending
[*] A > B     返回 NSOrderedSame
[*]A == B   返回 NSOrderedDescending
NSDate * nowDate = ;=   
NSDate * nowDateAfter10Seconds = ;
NSComparisonResultcomparisonResult = ;
NSLog(@"比较结果:%ld",comparisonResult ); 五、获取时间间隔

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

   - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;        
        返回和指定日期的时间间隔,单位是秒。
NSDate * nowDate = ;
NSDate * nowDateAfter10Seconds = ;
NSTimeInterval timeInterval = ;
NSLog(@"nowDate和nowDateAfter10Seconds间隔%lf秒",timeInterval); 2.间隔如今的时间间隔

   @property (readonly) NSTimeInterval timeIntervalSinceNow;
        返回指定日期间隔如今的时间间隔。
NSDate * nowDate = ;   
NSDate * nowDateAfter10Seconds = ;
NSTimeInterval timeInterval = nowDateAfter10Seconds.timeIntervalSinceNow;
NSLog(@"timeInterval:%lf秒",timeInterval); 3.间隔2001年的时间间隔

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

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

   - (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;   
        返回指定秒数之后的时间。
NSDate * nowDate = ;
NSDate * nowDateAfter = ;
NSLog(@"nowDateAfter:%@",nowDateAfter);


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