主要是对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的时间。
三、NSDateFormatter 日期格式化类
NSDateFormatter的日期格式如下:
G --纪元
一般会显示公元前(BC)和公元(AD)
y --年
假如是2013年,那么yyyy=2013,yy=13
M --月
假如是3月,那么M=3,MM=03,MMM=Mar,MMMM=March
假如是11月,那么M=11,MM=11,MMM=Nov,MMMM=November
w --年包含的周
假如是1月8日,那么w=2(这一年的第二个周)
W -- 月份包含的周(与日历排列有关)
假如是2013年4月21日,那么W=4(这个月的第四个周)
F -- 月份包含的周(与日历排列无关)
和上面的W不一样,F只是单纯以7天为一个单位来统计周,例如7号一定是第一个周,15号一定是第三个周,与日历排列无关。
D -- 年包含的天数
假如是1月20日,那么D=20(这一年的第20天)
假如是2月25日,那么D=31+25=56(这一年的第56天)
d -- 月份包含的天数
假如是5号,那么d=5,dd=05
假如是15号,那么d=15,dd=15
E --星期
假如是星期五,那么E=Fri,EEEE=Friday
a --上午(AM)/下午(PM)
H -- 24小时制,显示为0--23
假如是午夜00:40,那么H=0:40,HH=00:40
h -- 12小时制,显示为1--12
假如是午夜00:40,那么h=12:40
K -- 12小时制,显示为0--11
假如是午夜00:40,那么K=0:40,KK=00:40
k -- 24小时制,显示为1--24
假如是午夜00:40,那么k=24:40
m --分钟
假如是5分钟,那么m=5,mm=05
假如是45分钟,那么m=45,mm=45
s --秒
假如是5秒钟,那么s=5,ss=05
假如是45秒钟,那么s=45,ss=45
S --毫秒
一般用SSS来显示
z --时区
表现形式为GMT+08:00
Z --时区
表现形式为+0800
NSDateFormatter的两个最实用的方法是dateFromString和stringFromDate,前者将一个字符串经过格式化后变成NSDate对象,后者将NSDate对象格式化成字符串。
在调用setDateFormat设置格式化字符串时,可以加入一些别的字符串,用单引号来引入,例如:
[formatter setDateFormat:@"yyyy-MM-dd 'some ''special'' string' HH:mm:ss"];
使用NSDateFormatter转换时间字符串时,默认的时区是系统时区,例如在中国一般都是北京时间(+8),如果直接转换会导致结果相差8小时,所以一般的做法是先指定时区为GMT标准时间再转换,例如:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
NSDateComponents *compt = [[NSDateComponents alloc] init];
[compt setYear:2013];
[compt setMonth:3];
[compt setDay:13];
[compt setHour:1];
[compt setMinute:55];
[compt setSecond:28];
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [calendar dateFromComponents:compt];
NSLog(@"%@",date);
NSString *str = [formatter stringFromDate:date];
NSLog(@"%@",str);