一.NSDate的计算问题
NSTimeInterval 是一个以秒为单位的时间片。
1.可以用initWithTimeIntervalSinceNow方法传入一个NSTimeInterval对象,创建一个NSDate对象。
NSDate * tomorrow =[[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60];
NSDate * yesterday = [[NSDate alloc]initWithTimeIntervalSinceNow:-24*60*60];
2.可以使用+dateWithTimeIntervalSinceNow:方法来创建一个NSDate对象
NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];
NSDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow:24*60*60];
3.使用-dateByAddingTimeInterval方法创建NSDate对象
NSDate * now = [NSDate date];
NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
NSDate * anHourAfter = [now dateByAddingTimeInterval:60*60];
二、日期的比较
1.日期可以进行比较以确定大小或相等,也可以确定两个日期之间的时间间隔。两个日期的间隔时间差可以使用-timeIntervalSinceDate:方法来计算
NSDate * now = [NSDate date];
NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
NSTimeInterVal timeBetween = [now timeIntervalSinceDate:anHourAgo];
NSLog(@”%f”,timeBetween);
2.日期比较也可以使用-timeIntervalSinceNow方法获取和当前的时间间隔
NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60];
NSTimeInterval interval = [anHourAgo timeIntervalSinceNow];
NSLog(@”%f”,interval);
3.NSDate还提供了-laterDate、-earlierDate和compare方法来比较日期
NSDate * now = [NSDate date];
NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
NSDate *result1 = [now laterDate:anHourAgo];
NSDate * result2 = [now earlierDate:anHourAgo];
NSComparisonResult result3 = [now compare:anHourAgo];
三、时区问题
1. 处理日期和时间经常遇到的一个问题计算时区问题。Foundation框架提供NSTimeZone来指定日历对象的时区。+knowTimeZoneNamespace可以列举出所有时区;+timeZoneWithName可以指定名称参数创建一个时区;+timeZoneWithAbbreviation可以指定时区缩写创建一个时区
NSTimeZone * zone1 = [NSTimeZone timeZoneWithAbbreviation:@”PRC”];
NSTimeZone * zone2 = [NSTimeZone timeZoneWithName:@”Asia/Shanghai”];
2. 如果需要获取指定时区的时间字符串需要搭配NSDateFormatter来使用。NSDateFormatter可以将NSDate对象转换成所需的日期字符串
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];//分配内存,用以存放日期格式
[formatter setDateFormat:@”yyyy-MM-dd hh-mm-ss”];//定义格式
NSString * locationString = [formatter stringFromDate:[NSDate date]];//日期输出出来,用字符串进行接收
3.使用NSDateFormatter可以将字符串转换成NSDate类型。同样需要注意格式的问题。
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
NSString * dateStr = @”2013-04-25 16:23:55”;
NSDate * date = [formatter dateFromString:dateStr];//把字符串转换成Date格式
最后,不能为任意日期格式的字符串创建NSDateFormatter对象。
四、日历类
1. 在开始之前,我们应该告知系统我们当前的时间和日期。NSDate是取当前时间和日期的方法。
NSDate * date = [NSDate date];//定义当前系统时间
2. currentCalendar来获得当前系统用户设置的日历对象。
NSCalendar * currentCalendar = [NSCalendar currentCalendar];//设置当前的日历对象
3. 要从里面取值,还得定义其它的组件:NSComponents来表示一个日期对象的组件---例如年、月、日和小时。
NSDateComponents * components = [[NSDateComponents alloc]init];//定义里面的组件
4. 如果要使NSDateComponents对象有意义,必须将其与一个日历对象相关联。
NSDatecomponents * dateComponents = [currentCalents components: NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit| NSWeekCalendarUnit| NSWeekOfMonthCalendarUnit fromDate:date];//告知日历里面的组件究竟有哪些
也可以反过来使用
1. 先定义一个组件:
NSDateComponents * components = [[[NSDateComponents alloc]init]autorelease];//定义组件,并且自动释放
2. 对里面的组件进行赋值:
[components setYear:2013];
[components setMonth:4];
[components setDay:25];
[components setHour:16];
3. 定义一个日历对象:
NSCalendar *current = [[NSCalendar currentCalendar];
4. 定义一个日期对象,显示含有所定义组件的日期:
NSDate * date = [current dateFromComponents :compoments];