iOS NSDate的常用API

news/2024/7/20 20:20:59 标签: ios, objective-c

目录

一、创建日期

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时间),下面的示例展示了获取当前时间的方法。        

NSDate * todayDate = [NSDate date];
NSLog(@"当前时间:%@",todayDate);

2.当前时间指定秒数之后/前的时间

+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;

        这里返回的时候当前时间之后多少秒的时间。使用负数表示之前多少秒的时间。

NSDate * todayDate = [NSDate date];
NSLog(@"当前时间:%@",todayDate);
NSDate * afterTenSeconds = [NSDate dateWithTimeIntervalSinceNow:10];
NSLog(@"10秒钟之后的时间:%@",afterTenSeconds);
NSDate * beforeTenSeconds = [NSDate dateWithTimeIntervalSinceNow:-10];
NSLog(@"10秒钟之前的时间:%@",beforeTenSeconds);

3.指定日期之后/后的时间

+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;

        指定日期多少秒之后的时间,负数表示指定日期多少秒之前的时间。

NSDate * todayDate = [NSDate date];    
NSLog(@"当前时间:%@",todayDate);     
NSDate * afterTenSecondDate = [NSDate dateWithTimeInterval:10 sinceDate:todayDate];        
NSDate * beginTenSecondDate = [NSDate dateWithTimeInterval:-10 sinceDate:todayDate];   
NSLog(@"10秒钟之后的时间:%@",afterTenSecondDate);
NSLog(@"10秒钟之后的时间:%@",beginTenSecondDate);

4.2001年之后/前指定秒数的时间

+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;

        返回从 2001 年 1 月 1 日 00:00:00 UTC 开始的给定秒数之后的时间,负数表示给定秒数之前的时间。

NSDate * todayDate = [NSDate date];
NSLog(@"当前时间:%@",todayDate);
NSDate * tenSecondsAfter2001 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];  
NSLog(@"2001-1-1 00:00:00 之后10秒的时间当前时间:%@",tenSecondsAfter2001);

5.1970年之后/后指定秒数的时间

+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

        返回从 1970 年 1 月 1 日 00:00:00 UTC 开始的给定秒数之后的时间,负数表示给定秒数之前的时间。

NSDate * tenSecondsAfter1970 = [NSDate dateWithTimeIntervalSince1970:10];
NSLog(@"2001-1-1 00:00:00 之后10秒的时间当前时间:%@",tenSecondsAfter1970);

二、初始化日期

1.init

- (instancetype)init;

        这里返回的也是当前的时间

NSDate * nowDate = [[NSDate alloc]init];
NSLog(@"当前时间:%@",nowDate);

2.时间间指定秒数的时间

- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

NSDate * nowDate = [[NSDate alloc]init];    
NSLog(@"当前时间:%@",nowDate);
NSDate * afterTenSecondsDate = [[NSDate alloc]initWithTimeIntervalSinceNow:10];
NSLog(@"10秒钟之后的时间:%@",afterTenSecondsDate);

3.指定时间指定秒数之前/后的时间

- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;

NSDate * nowDate = [[NSDate alloc]init];        
NSLog(@"当前时间:%@",nowDate);
NSDate * afterTenSecondsDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:nowDate];        
NSLog(@"10秒钟之后的时间:%@",afterTenSecondsDate);

4.2001年指定秒数之后/前的时间

- (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;

NSDate * nowDate = [[NSDate alloc]init];        
NSLog(@"当前时间:%@",nowDate);        
NSDate * afterTenSecondsFrom2001 = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:10];        
NSLog(@"2001年10秒钟之后的时间:%@",afterTenSecondsFrom2001);

5.1970年指定秒数之后/前的时间

- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;

NSDate * nowDate = [[NSDate alloc]init];        
NSLog(@"当前时间:%@",nowDate);        
NSDate * afterTenSecondsFrom1970 = [[NSDate alloc]initWithTimeIntervalSince1970:10];        
NSLog(@"1970年10秒钟之后的时间:%@",afterTenSecondsFrom1970);

三、获取时间边界

1.distantFuture

2.distantPast

四、时间比较

1.比较日期是否相等

- (BOOL)isEqualToDate:(NSDate *)otherDate;

        比较A日期是否和B日期是否相同。相同返回YES,不同返回NO。

NSDate * nowDate = [NSDate now];
NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];
if ([nowDate isEqualToDate:nowDateAfter10Seconds]) {
    NSLog(@"nowDate == nowDateAfter10Seconds");
} else {
    NSLog(@"nowDate != nowDateAfter10Seconds");
}
if ([[NSDate now] isEqualToDate:[[NSDate alloc]init]]) {
    NSLog(@"equal");
}

2.比较日期是否较早

- (NSDate *)earlierDate:(NSDate *)anotherDate;

        比较A日期是否早于B日期,是的话返回YES,否则返回NO。

NSDate * nowDate = [NSDate now];
NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];
if ([nowDate isEarlierThanOrEqualTo:nowDateAfter10Seconds]) {
     NSLog(@"nowDate isEarlierThanOrEqualTo nowDateAfter10Seconds"); 
} else {
    NSLog(@"nowDate isNotEarlierThanOrEqualTo nowDateAfter10Seconds");
}

3.比较日期是否较晚

- (NSDate *)laterDate:(NSDate *)anotherDate;

        比较A日期是否晚与B日期和上面的返回结果刚好相反。

    
NSDate * nowDate = [NSDate now];
NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];    
if ([nowDate isLaterThan:nowDateAfter10Seconds]) {
   NSLog(@"nowDate isLaterThan nowDateAfter10Seconds");    
} else {
    NSLog(@"nowDate isNotLaterThan nowDateAfter10Seconds");
}

4.带回调的日期比较的方法

- (NSComparisonResult)compare:(NSDate *)other;

        这里这个方法的返回值是NSComparisonResult类型的枚举,当两个日期完全相同的时候返回NSOrderedSame。  

  1. A < B     返回 NSOrderedAscending
  2. A > B     返回 NSOrderedSame

  3. A == B   返回 NSOrderedDescending
NSDate * nowDate = [NSDate now];=   
NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10];  
NSComparisonResult  comparisonResult = [nowDate compare:nowDateAfter10Seconds];
NSLog(@"比较结果:%ld",comparisonResult );

五、获取时间间隔

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

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;        

        返回和指定日期的时间间隔,单位是秒。

NSDate * nowDate = [NSDate now];
NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10]; 
NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:nowDateAfter10Seconds]; 
NSLog(@"nowDate和nowDateAfter10Seconds间隔%lf秒",timeInterval);

2.距离现在的时间间隔

@property (readonly) NSTimeInterval timeIntervalSinceNow;

        返回指定日期距离现在的时间间隔。

NSDate * nowDate = [NSDate now];    
NSDate * nowDateAfter10Seconds = [NSDate dateWithTimeIntervalSinceNow:10]; 
NSTimeInterval timeInterval = nowDateAfter10Seconds.timeIntervalSinceNow;  
NSLog(@"timeInterval:%lf秒",timeInterval);

3.距离2001年的时间间隔

@property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;        

        返回指定日期距离2001年1月1日的时间间距,单位是秒。

NSDate * nowDate = [NSDate now];    
NSTimeInterval timeInterval = nowDate.timeIntervalSinceReferenceDate;    
NSLog(@"timeInterval:%lf秒",timeInterval);

4.距离1970年的时间间隔 

@property (readonly) NSTimeInterval timeIntervalSince1970; 

        返回指定日期距离1970年1月1日的时间间距,单位是秒。

NSDate * nowDate = [NSDate now];    
NSTimeInterval timeInterval = nowDate.timeIntervalSince1970;    
NSLog(@"timeInterval:%lf秒",timeInterval);

六、时间的运算

- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;   

        返回指定秒数之后的时间。

NSDate * nowDate = [NSDate now];
NSDate * nowDateAfter = [nowDate dateByAddingTimeInterval:24 * 60 * 60];
NSLog(@"nowDateAfter:%@",nowDateAfter);


http://www.niftyadmin.cn/n/5226659.html

相关文章

计算机网络 一到二章 PPT 复习

啥币老师要隔段时间测试&#xff0c;我只能说坐胡狗吧旁边 第一章 这nm真的会考&#xff0c;我是绷不住的 这nm有五种&#xff0c;我一直以为只有三种 广播帧在后面的学习中经常遇到 虽然老师在上课的过程中并没有太过强调TCP/IP的连接和断开&#xff0c;但我必须强调一下&…

【分布式学习】之架构、系统、集群部署

分布式架构 分布式架构是指在网络环境下&#xff0c;将一个应用或系统拆分成多个模块&#xff0c;将它们部署在不同的计算机节点上&#xff0c;并通过网络协议进行通信和协作。每个节点都可以独立地进行处理&#xff0c;并且它们通过相互通信来完成任务&#xff0c;从而提高整…

NoSQL大数据存储技术测试题(参考答案)

目录 1.绪论 2.NoSQL数据库的基本原理 4.HBase的基本原理与使用 5.HBase高级原理 7.MongoDB 8.其他NoSQL数据库 1.绪论 总分: 14.0 10分 单项选择题 4分 判断题 教师评语&#xff1a; 一 单项选择题(10分) 1、NoSQL一词表示的含义是&#xff08;&#xff09;。&#xf…

使用Postman创建Mock Server

这篇文章将教会大家如何利用 Postman&#xff0c;通过 Mock 的方式测试我们的 API。 什么是 Mock Mock 是一项特殊的测试技巧&#xff0c;可以在没有依赖项的情况下进行单元测试。通常情况下&#xff0c;Mock 与其他方法的主要区别就是&#xff0c;用于取代代码依赖项的模拟对…

C语言--不创建第三个变量,实现对两个数字的交换

我们先来看一下&#xff0c;创建临时变量交换两个数字。 #include<stdio.h> {int a2;int b3;int tmp0;printf("before:a%d b%d\n",a,b);tmpa;ab;btmp;printf("after:a%d b%d\n",a,b);return 0; } 图解&#xff1a; 运行结果&#xff1a; 再看一下不…

kafka C++实现生产者

文章目录 1 Kafka 生产者的逻辑2 Kafka 的C API2.1 RdKafka::Conf2.2 RdKafka::Message2.3 RdKafka::DeliveryReportCb2.4 RdKafka::Event2.5 RdKafka::EventCb2.6 RdKafka::PartitionerCb2.7 RdKafka::Topic2.8 RdKafka::Producer&#xff08;核心&#xff09; 3 Kafka 生产者…

纯js实现录屏并保存视频到本地的尝试

前言&#xff1a;先了解下&#xff1a;navigator.mediaDevices&#xff0c;mediaDevices 是 Navigator 只读属性&#xff0c;返回一个 MediaDevices 对象&#xff0c;该对象可提供对相机和麦克风等媒体输入设备的连接访问&#xff0c;也包括屏幕共享。 const media navigator…

如何跑通跨窗口渲染:multipleWindow3dScene

New 这是一个跨窗口渲染的示例&#xff0c;用 Three.js 和 localStorage 在同一源&#xff08;同产品窗口&#xff09;上跨窗口设置 3D 场景。而这也是本周推特和前端圈的一个热点&#xff0c;有不少人在争相模仿它的实现&#xff0c;如果你对跨窗口的渲染有兴趣&#xff0c;可…