NSDate的計算問題、日期計算、時區問題、NSTimer

一.NSDate的計算問題   框架

   NSTimeInterval 是一個以秒爲單位的時間片。 spa

1.能夠用initWithTimeIntervalSinceNow方法傳入一個NSTimeInterval對象,建立一個NSDate對象。 代理

  NSDate * tomorrow =[[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60]; orm

  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對象 get

  NSDate * now = [NSDate date]; string

  NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];

  NSDate * anHourAfter = [now dateByAddingTimeInterval:60*60];

2、日期的比較

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];

3、時區問題:

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對象。

4、NSTimer

 1. NSTimer是Cocoa中比較經常使用的定時器類,它能夠完成定時功能。使用NSTimer須要記住三要素:

   ---時間間隔NSTimeInterval爲浮點型

   ---事件代理delegate

   ---事件處理方法@selector

2.常使用+scheduledTimerWithTimeInterval:  target:  selector:  userInfo:  repeat:  方法建立

  參數說明:

--Interval設定x秒後啓動定時器;

--target參數是指執行第三個參數方法的對象

--selector指定了定時器觸發調用的方法;

--userInfo指定了定時器的用戶信息,能夠設置成nil,也能夠經過這個參數傳值

--repeat參數若是設置成YES,定時器將會按照預約的時間重複執行,若是設置成NO,定時器對象啓動一次後再也不重複執行。

  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);

}

相關文章
相關標籤/搜索