主要是对ios平台NSDate时间类中的方法的用法解释,以备不时之需:
NSDate用来表示公历的GMT时间(格林威治时间)。 有下面几种初始化方法:
1. - (id)init
默认初始化,返回当前时间,也可以直接调用类方法 +(id)date
NSDate *date = [[NSDate alloc] init];
//NSDate *date = [NSDate date];
NSLog(@"print date is %@",date);
将打印出计算机当前时间:2022-01-20 15:30:20 +0000
2. - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)seconds
以当前时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:20];
//NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];
NSLog(@"print date is %@",date);
假如当前时间是2022-01-20 08:57:20 +0000,那么初始化后得到的时间是2022-01-20 08:57:40 +0000
3. - (id)initWithTimeIntervalSince1970:(NSTimeInterval)seconds
以GMT时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:-20];
//NSDate *date = [NSDate dateWithTimeIntervalSince1970:-20];
NSLog(@"print date is %@",date);
得到的时间是格林威治时间往前20秒,将打印出:1969-12-31 23:59:40 +0000
4. - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds
以2001-1-1 0:0:0的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:80];
//NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:80];
NSLog(@"print date is %@",date);
将打印出:2001-01-01 00:01:20 +0000
5. - (id)initWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)refDate
以基准时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)date
NSDate *date1 = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:20];
NSLog(@"print date1 is %@",date1);
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:10 sinceDate:date1];
//NSDate *date2 = [NSDate dateWithTimeInterval:10 sinceDate:date1];
NSLog(@"print date2 is %@",date2);
第一个基准时间是2001-01-01 00:00:20 +0000,根据基准时间偏移10秒的结果是2001-01-01 00:00:30 +0000
6. + (id)distantPast 与 + (id)distantFuture
这两个是类方法,分别用来返回一个极早的时间点和一个极晚的时间点
NSDate *date = [NSDate distantFuture];
NSLog(@"future date is %@",date);
NSDate *date2 = [NSDate distantPast];
NSLog(@"past date is %@",date2);
distantPast将返回:0001-12-30 00:00:00 +0000,distantFuture将返回:4001-01-01 00:00:00 +0000
NSDate的常用对象方法:
1. -(id)dateByAddingTimeInterval:(NSTimeInterval)seconds
返回以当前NSDate对象为基准,偏移多少秒后得到的新NSDate对象。(旧方法 - (id)addTimeInterval:(NSTimeInterval)seconds已被弃用)
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [date dateByAddingTimeInterval:-20];
NSLog(@"%@",date2);
2. - (BOOL)isEqualToDate:(NSDate *)anotherDate
将当前对象与参数传递的对象进行比较,根据是否相同返回BOOL值
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:0 sinceDate:date];
BOOL isEqual = [date isEqualToDate:date2];
NSLog(@"%i",isEqual);
3. - (NSDate *)earlierDate:(NSDate *)anotherDate 与 - (NSDate *)laterDate:(NSDate *)anotherDate
比较两个NSDate对象,返回较早/较晚的时间点,并以新NSDate对象的形式返回
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:-50 sinceDate:date];
NSDate *date3 = [date earlierDate:date2];
NSLog(@"earlier date is %@",date3);
NSDate *date4 = [date laterDate:date2];
NSLog(@"later date is %@",date4);
4. - (NSComparisonResult)compare:(NSDate *)anotherDate
将当前对象与参数传递的对象进行比较,
如果相同,返回0(NSOrderedSame);
对象时间早于参数时间,返回-1(NSOrderedAscending);
对象时间晚于参数时间,返回1(NSOrderedDescending)
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:-50 sinceDate:date];
NSInteger result = [date compare:date2];
NSLog(@"%i",result);
5. - (NSTimeInterval)timeIntervalSince1970
返回当前对象时间与1970-1-1 0:0:0的相隔秒数,也可以这样理解:从1970-1-1 0:0:0开始,经过多少秒到达对象指定时间。
NSDate *date = [NSDate dateWithTimeIntervalSince1970:100];
NSInteger seconds = [date timeIntervalSince1970];
NSLog(@"%i",seconds);
将返回结果100
6. - (NSTimeInterval)timeIntervalSinceReferenceDate
返回当前对象时间与2001-1-1 0:0:0的相隔秒数,也可以这样理解:从2001-1-1 0:0:0开始,经过多少秒到达对象指定时间。
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:-30];
NSInteger seconds = [date timeIntervalSinceReferenceDate];
NSLog(@"%i",seconds);
将返回结果-30,负数代表从2001-1-1 0:0:0开始,倒退30秒到达当前时间。
7. - (NSTimeInterval)timeIntervalSinceNow
返回当前对象时间与客户端时间的相隔秒数,也可以这样理解:从客户端当前时间开始,经过多少秒到达对象指定时间。
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:100];
NSInteger seconds = [date timeIntervalSinceNow];
NSLog(@"%i",seconds);
经测试返回了结果99,但初始化时提供的参数是100。这可能是因为第一句初始化代码到第二句计算代码之间有个1秒内的延时,所以计算时的客户端时间比初始化时的客户端时间快了1秒。
8. - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
返回当前对象时间与参数传递的对象时间的相隔秒数,也可以这样理解:从参数时间开始,经过多少秒到达对象执行时间。
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:50 sinceDate:date];
NSInteger seconds = [date timeIntervalSinceDate:date2];
NSLog(@"%i",seconds);
将返回结果-50,date为1970-1-1 0:0:0,date2为1970-1-1 0:0:50,从date2的时间开始,倒退50秒到达date的时间。