成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

一.NSDate的計算問題  

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

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

 

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

 

2.可以使用+dateWithTimeIntervalSinceNow:方法來創(chuàng)建一個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.日期可以進行比較以確定大小或相等,也可以確定兩個日期之間的時間間隔。兩個日期的間隔時間差可以使用-timeIntervalSinceDate:方法來計算

 

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

 

2.日期比較也可以使用-timeIntervalSinceNow方法獲取和當前的時間間隔

 

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

 

三、時區(qū)問題:

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

 

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

 

2.  如果需要獲取指定時區(qū)的時間字符串需要搭配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]];//日期輸出出來,用字符串進行接收 

 

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中比較常用的定時器類,它可以完成定時功能。使用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的使用示例

 

  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. }  

 

責任編輯:閆佳明 來源: oschina
相關推薦

2019-09-20 08:47:57

DockerLinux軟件

2013-08-21 10:46:02

DatePicker時區(qū)

2012-06-15 11:18:07

云安全云計算

2011-07-22 10:06:44

云計算成本

2013-04-02 11:07:16

2009-10-15 11:23:17

云計算

2010-05-06 17:48:50

云計算

2018-10-10 21:00:50

2012-08-21 10:17:04

2010-09-24 15:46:23

SQL查詢

2010-06-09 08:10:49

2013-12-19 09:49:42

云計算集成云集成云連接器

2020-03-03 09:28:30

Python內(nèi)存開發(fā)

2013-07-23 10:13:17

2022-01-14 11:48:39

量子計算硬件技術

2011-07-01 09:46:44

云計算遷移

2012-07-30 09:49:44

云計算

2012-12-12 15:19:32

云安全

2020-08-27 15:16:28

邊緣計算

2010-11-17 10:14:35

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 中文字幕亚洲区 | 国产一级毛片精品完整视频版 | 久久成人免费观看 | 先锋av资源网 | 一区不卡在线观看 | a免费视频| 蜜桃视频在线观看免费视频网站www | 国产日韩欧美一区 | 黄色成人在线网站 | 麻豆hd| 日韩精品成人免费观看视频 | 国产激情在线 | 成人欧美一区二区三区黑人孕妇 | 中文在线a在线 | 亚洲一二三视频 | 午夜码电影 | 影音先锋久久 | 久久久久久久一区 | 日韩免费视频一区二区 | 97伦理影院 | 午夜伦4480yy私人影院 | 日韩在线小视频 | 香蕉久久久 | 在线观看免费福利 | 久久久久久亚洲国产精品 | 日韩精品一区二区三区在线观看 | 中文字幕日韩欧美 | 欧美激情一区二区 | 黄免费观看 | 日韩成人高清在线 | 欧美xxxx网站 | 亚洲综合视频 | 日本一二三区高清 | 国产精品一区在线 | 久久天堂网 | 亚洲激情一级片 | 在线不卡视频 | 久久久久久久久久久久久9999 | 国产高清在线精品 | 在线观看免费观看在线91 | 中文字幕精品一区二区三区精品 |