自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

NSDate的計(jì)算問題,日期計(jì)算,時(shí)區(qū)問題,NSTimer

移動(dòng)開發(fā) iOS
本文介紹了NSDate的計(jì)算問題,日期計(jì)算,時(shí)區(qū)問題,NSTimer等問題的具體使用方法和注意事項(xiàng)。

一.NSDate的計(jì)算問題  

NSTimeInterval 是一個(gè)以秒為單位的時(shí)間片。

1.可以用initWithTimeIntervalSinceNow方法傳入一個(gè)NSTimeInterval對象,創(chuàng)建一個(gè)NSDate對象。

 

  1. NSDate * tomorrow =[[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60]; 
  2. SDate * yesterday = [[NSDate alloc]initWithTimeIntervalSinceNow:-24*60*60];  

 

2.可以使用+dateWithTimeIntervalSinceNow:方法來創(chuàng)建一個(gè)NSDate對象

 

  1. NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60]; 
  2. SDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow:24*60*60];  

 

3.使用-dateByAddingTimeInterval方法創(chuàng)建NSDate對象

 

  1. NSDate * now = [NSDate date]; 
  2. NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60]; 
  3. NSDate * anHourAfter = [now dateByAddingTimeInterval:60*60];  

 

二、日期的比較

1.日期可以進(jìn)行比較以確定大小或相等,也可以確定兩個(gè)日期之間的時(shí)間間隔。兩個(gè)日期的間隔時(shí)間差可以使用-timeIntervalSinceDate:方法來計(jì)算

 

  1. NSDate * now = [NSDate date]; 
  2. NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60]; 
  3. NSTimeInterVal timeBetween = [now timeIntervalSinceDate:anHourAgo]; 
  4. NSLog(@”%f”,timeBetween);  

 

2.日期比較也可以使用-timeIntervalSinceNow方法獲取和當(dāng)前的時(shí)間間隔

 

  1. NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60]; 
  2. NSTimeInterval interval = [anHourAgo timeIntervalSinceNow]; 
  3. NSLog(@”%f”,interval);  

 

3.NSDate還提供了-laterDate、-earlierDate和compare方法來比較日期

 

  1. NSDate * now = [NSDate date]; 
  2. NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60]; 
  3. NSDate *result1 = [now laterDate:anHourAgo]; 
  4. NSDate * result2 = [now earlierDate:anHourAgo]; 
  5. NSComparisonResult result3 = [now compare:anHourAgo];  

 

三、時(shí)區(qū)問題:

1.  處理日期和時(shí)間經(jīng)常遇到的一個(gè)問題計(jì)算時(shí)區(qū)問題。Foundation框架提供NSTimeZone來指定日 歷對象的時(shí)區(qū)。+knowTimeZoneNamespace可以列舉出所有時(shí)區(qū);+timeZoneWithName可以指定名稱參數(shù)創(chuàng)建一個(gè)時(shí) 區(qū);+timeZoneWithAbbreviation可以指定時(shí)區(qū)縮寫創(chuàng)建一個(gè)時(shí)區(qū)

 

  1. NSTimeZone * zone1 = [NSTimeZone timeZoneWithAbbreviation:@”PRC”]; 
  2. STimeZone * zone2 = [NSTimeZone timeZoneWithName:@”Asia/Shanghai”];  

 

2.  如果需要獲取指定時(shí)區(qū)的時(shí)間字符串需要搭配NSDateFormatter來使用。NSDateFormatter可以將NSDate對象轉(zhuǎn)換成所需的日期字符串

 

  1. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];//分配內(nèi)存,用以存放日期格式 
  2. [formatter setDateFormat:@”yyyy-MM-dd hh-mm-ss”];//定義格式 
  3. NSString * locationString = [formatter stringFromDate:[NSDate date]];//日期輸出出來,用字符串進(jìn)行接收 

 

3.使用NSDateFormatter可以將字符串轉(zhuǎn)換成NSDate類型。同樣需要注意格式的問題。

 

  1. NSDateFormatter * formatter = [[NSDateFormatter alloc]init]; 
  2. [formatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”]; 
  3. NSString * dateStr = @”2013-04-25 16:23:55”; 
  4. NSDate * date = [formatter dateFromString:dateStr];//把字符串轉(zhuǎn)換成Date格式  

 

最后,不能為任意日期格式的字符串創(chuàng)建NSDateFormatter對象。

四、NSTimer

1. NSTimer是Cocoa中比較常用的定時(shí)器類,它可以完成定時(shí)功能。使用NSTimer需要記住三要素:

---時(shí)間間隔NSTimeInterval為浮點(diǎn)型

---事件代理delegate

---事件處理方法@selector

2.常使用+scheduledTimerWithTimeInterval:  target:  selector:  userInfo:  repeat:  方法創(chuàng)建

參數(shù)說明:

--Interval設(shè)定x秒后啟動(dòng)定時(shí)器;

--target參數(shù)是指執(zhí)行第三個(gè)參數(shù)方法的對象

--selector指定了定時(shí)器觸發(fā)調(diào)用的方法;

--userInfo指定了定時(shí)器的用戶信息,可以設(shè)置成nil,也可以通過這個(gè)參數(shù)傳值

--repeat參數(shù)如果設(shè)置成YES,定時(shí)器將會(huì)按照預(yù)定的時(shí)間重復(fù)執(zhí)行,如果設(shè)置成NO,定時(shí)器對象啟動(dòng)一次后不再重復(fù)執(zhí)行。

NSTimer的使用示例

 

  1.  NSTimer *timer=[NSTimer  scheduledTimerWithTimeInterval: 1.0 target : self  selector:@selector(showTime:)useInfo:nil repeat:NO]; 
  2. -(void)showTime:(NSTimer *)theTimer{ 
  3. NSDateFormatter * formatter = [[NSDateFormatter alloc]init]; 
  4.    [formatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”]; 
  5.    NSString * date = [formatter stringFromDate:[NSDate date]]; 
  6.    if([date isEqualToString:@”2013-04-25 16:46:10”]) 
  7. [timer invalidate]; 
  8. NSLog(@”date:%@”,date); 
  9. }  

 

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2013-08-21 10:46:02

DatePicker時(shí)區(qū)

2019-09-20 08:47:57

DockerLinux軟件

2012-06-15 11:18:07

云安全云計(jì)算

2011-07-22 10:06:44

云計(jì)算成本

2010-05-06 17:48:50

云計(jì)算

2009-10-15 11:23:17

云計(jì)算

2010-09-24 15:46:23

SQL查詢

2012-08-21 10:17:04

2013-04-02 11:07:16

2018-10-10 21:00:50

2010-06-09 08:10:49

2013-12-19 09:49:42

云計(jì)算集成云集成云連接器

2010-11-17 10:14:35

2012-08-14 10:21:29

2011-07-01 09:46:44

云計(jì)算遷移

2012-12-12 15:19:32

云安全

2013-07-23 10:13:17

2012-07-30 09:49:44

云計(jì)算

2022-01-14 11:48:39

量子計(jì)算硬件技術(shù)

2020-03-03 09:28:30

Python內(nèi)存開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號