NSDate的計算問題,日期計算,時區(qū)問題,NSTimer
一.NSDate的計算問題
NSTimeInterval 是一個以秒為單位的時間片。
1.可以用initWithTimeIntervalSinceNow方法傳入一個NSTimeInterval對象,創(chuàng)建一個NSDate對象。
- NSDate * tomorrow =[[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60];
- SDate * yesterday = [[NSDate alloc]initWithTimeIntervalSinceNow:-24*60*60];
2.可以使用+dateWithTimeIntervalSinceNow:方法來創(chuàng)建一個NSDate對象
- NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];
- SDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow:24*60*60];
3.使用-dateByAddingTimeInterval方法創(chuàng)建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];
三、時區(qū)問題:
1. 處理日期和時間經(jīng)常遇到的一個問題計算時區(qū)問題。Foundation框架提供NSTimeZone來指定日 歷對象的時區(qū)。+knowTimeZoneNamespace可以列舉出所有時區(qū);+timeZoneWithName可以指定名稱參數(shù)創(chuàng)建一個時 區(qū);+timeZoneWithAbbreviation可以指定時區(qū)縮寫創(chuàng)建一個時區(qū)
- NSTimeZone * zone1 = [NSTimeZone timeZoneWithAbbreviation:@”PRC”];
- STimeZone * zone2 = [NSTimeZone timeZoneWithName:@”Asia/Shanghai”];
2. 如果需要獲取指定時區(qū)的時間字符串需要搭配NSDateFormatter來使用。NSDateFormatter可以將NSDate對象轉(zhuǎn)換成所需的日期字符串
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];//分配內(nèi)存,用以存放日期格式
- [formatter setDateFormat:@”yyyy-MM-dd hh-mm-ss”];//定義格式
- NSString * locationString = [formatter stringFromDate:[NSDate date]];//日期輸出出來,用字符串進行接收
3.使用NSDateFormatter可以將字符串轉(zhuǎn)換成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];//把字符串轉(zhuǎn)換成Date格式
最后,不能為任意日期格式的字符串創(chuàng)建NSDateFormatter對象。
四、NSTimer
1. NSTimer是Cocoa中比較常用的定時器類,它可以完成定時功能。使用NSTimer需要記住三要素:
---時間間隔NSTimeInterval為浮點型
---事件代理delegate
---事件處理方法@selector
2.常使用+scheduledTimerWithTimeInterval: target: selector: userInfo: repeat: 方法創(chuàng)建
參數(shù)說明:
--Interval設定x秒后啟動定時器;
--target參數(shù)是指執(zhí)行第三個參數(shù)方法的對象
--selector指定了定時器觸發(fā)調(diào)用的方法;
--userInfo指定了定時器的用戶信息,可以設置成nil,也可以通過這個參數(shù)傳值
--repeat參數(shù)如果設置成YES,定時器將會按照預定的時間重復執(zhí)行,如果設置成NO,定時器對象啟動一次后不再重復執(zhí)行。
NSTimer的使用示例
- NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval: 1.0 target : self selector:@selector(showTime:)useInfo:nil repeat:NO];
- -(void)showTime:(NSTimer *)theTimer{
- NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
- NSString * date = [formatter stringFromDate:[NSDate date]];
- if([date isEqualToString:@”2013-04-25 16:46:10”])
- [timer invalidate];
- NSLog(@”date:%@”,date);
- }