生活随笔
收集整理的這篇文章主要介紹了
iphone——日期处理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://blog.csdn.net/lingedeng/article/details/6996599
Dates
????????NSDate類提供了創(chuàng)建date,比較date以及計算兩個date之間間隔的功能。Date對象是不可改變的。
????????如果你要創(chuàng)建date對象并表示當前日期,你可以alloc一個NSDate對象并調(diào)用init初始化:
?
[cpp]?view plaincopy
NSDate?*now?=?[[NSDate?alloc]?init];??
????????或者使用NSDate的date類方法來創(chuàng)建一個日期對象。如果你需要與當前日期不同的日期,你可以使用NSDate的initWithTimeInterval...或dateWithTimeInterval...方法,你也可以使用更復雜的calendar或date components對象。
?
????????創(chuàng)建一定時間間隔的NSDate對象:
?
[plain]?view plaincopy
NSTimeInterval?secondsPerDay?=?24?*?60?*?60;????NSDate?*tomorrow?=?[[NSDate?alloc]?initWithTimeIntervalSinceNow:secondsPerDay];????NSDate?*yesterday?=?[[NSDate?alloc]?initWithTimeIntervalSinceNow:-secondsPerDay];????[tomorrow?release];??[yesterday?release];??
????????使用增加時間間隔的方式來生成NSDate對象:
?
?
[plain]?view plaincopy
NSTimeInterval?secondsPerDay?=?24?*?60?*?60;????NSDate?*today?=?[[NSDate?alloc]?init];??NSDate?*tomorrow,?*yesterday;????tomorrow?=?[today?dateByAddingTimeInterval:?secondsPerDay];??yesterday?=?[today?dateByAddingTimeInterval:?-secondsPerDay];????[today?release];??
????????如果要對NSDate對象進行比較,可以使用isEqualToDate:, compare:, laterDate:和 earlierDate:方法。這些方法都進行精確比較,也就是說這些方法會一直精確比較到NSDate對象中秒一級。例如,你可能比較兩個日期,如果他們之間的間隔在一分鐘之內(nèi)則認為這兩個日期是相等的。在這種情況下使用,timeIntervalSinceDate:方法來對兩個日期進行比較。下面的代碼進行了示例:
?
?
[plain]?view plaincopy
if?(fabs([date2?timeIntervalSinceDate:date1])?<?60)?...?? ?
?
NSCalendar & NSDateComponents
????????日歷對象封裝了對系統(tǒng)日期的計算,包括這一年開始,總天數(shù)以及劃分。你將使用日歷對象對絕對日期與date components(包括年,月,日,時,分,秒)進行轉換。
????????NSCalendar定義了不同的日歷,包括佛教歷,格里高利歷等(這些都與系統(tǒng)提供的本地化設置相關)。NSCalendar與NSDateComponents對象緊密相關。
????????你可以通過NSCalendar對象的currentCalendar方法來獲得當前系統(tǒng)用戶設置的日歷。
?
[plain]?view plaincopy
NSCalendar?*currentCalendar?=?[NSCalendar?currentCalendar];????NSCalendar?*japaneseCalendar?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSJapaneseCalendar];????NSCalendar?*usersCalendar?=?[[NSLocale?currentLocale]?objectForKey:NSLocaleCalendar];?? ????????usersCalendar和currentCalendar對象是相等的,盡管他們是不同的對象。
?
????????你可以使用NSDateComponents對象來表示一個日期對象的組件——例如年,月,日和小時。如果要使一個NSDateComponents對象有意義,你必須將其與一個日歷對象相關聯(lián)。下面的代碼示例了如何創(chuàng)建一個NSDateComponents對象:
?
[plain]?view plaincopy
NSDateComponents?*components?=?[[NSDateComponents?alloc]?init];????[components?setDay:6];??[components?setMonth:5];??[components?setYear:2004];????NSInteger?weekday?=?[components?weekday];?//?Undefined?(==?NSUndefinedDateComponent)??
????????要將一個日期對象解析到相應的date components,你可以使用NSCalendar的components:fromDate:方法。此外日期本身,你需要指定NSDateComponents對象返回組件。
?
?
[plain]?view plaincopy
NSDate?*today?=?[NSDate?date];????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????NSDateComponents?*weekdayComponents?=?[gregorian?components:(NSDayCalendarUnit?|?NSWeekdayCalendarUnit)?fromDate:today];????NSInteger?day?=?[weekdayComponents?day];??NSInteger?weekday?=?[weekdayComponents?weekday];????同樣你也可以從NSDateComponents對象來創(chuàng)建NSDate對象:??NSDateComponents?*components?=?[[NSDateComponents?alloc]?init];????[components?setWeekday:2];?//?Monday??[components?setWeekdayOrdinal:1];?//?The?first?Monday?in?the?month??[components?setMonth:5];?//?May??[components?setYear:2008];????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????NSDate?*date?=?[gregorian?dateFromComponents:components];??
????????為了保證正確的行為,您必須確保使用的組件在日歷上是有意義的。指定“出界”日歷組件,如一個-6或2月30日在公歷中的日期值產(chǎn)生未定義的行為。
?
????????你也可以創(chuàng)建一個不帶年份的NSDate對象,這樣的操作系統(tǒng)會自動生成一個年份,但在后面的代碼中不會使用其自動生成的年份。
?
[plain]?view plaincopy
NSDateComponents?*components?=?[[NSDateComponents?alloc]?init];????[components?setMonth:11];??[components?setDay:7];????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????NSDate?*birthday?=?[gregorian?dateFromComponents:components];??
??????? 下面的示例顯示了如何從一個日歷置換到另一個日歷:
?
?
[plain]?view plaincopy
NSDateComponents?*comps?=?[[NSDateComponents?alloc]?init];????[comps?setDay:6];??[comps?setMonth:5];??[comps?setYear:2004];????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????NSDate?*date?=?[gregorian?dateFromComponents:comps];????[comps?release];??[gregorian?release];????NSCalendar?*hebrew?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSHebrewCalendar];????NSUInteger?unitFlags?=?NSDayCalendarUnit?|?NSMonthCalendarUnit?|?NSYearCalendarUnit;????NSDateComponents?*components?=?[hebrew?components:unitFlags?fromDate:date];????NSInteger?day?=?[components?day];?//?15??NSInteger?month?=?[components?month];?//?9??NSInteger?year?=?[components?year];?//?5764?? ?
?
歷法計算
????????在當前時間加上一個半小時:
[plain]?view plaincopy
NSDate?*today?=?[[NSDate?alloc]?init];????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????NSDateComponents?*offsetComponents?=?[[NSDateComponents?alloc]?init];????[offsetComponents?setHour:1];??[offsetComponents?setMinute:30];????//?Calculate?when,?according?to?Tom?Lehrer,?World?War?III?will?end??NSDate?*endOfWorldWar3?=?[gregorian?dateByAddingComponents:offsetComponents?toDate:today?options:0];??
????????獲得當前星期中的星期天(使用格里高利歷):
?
?
[plain]?view plaincopy
NSDate?*today?=?[[NSDate?alloc]?init];????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????//?Get?the?weekday?component?of?the?current?date??NSDateComponents?*weekdayComponents?=?[gregorian?components:NSWeekdayCalendarUnit?fromDate:today];????/*??Create?a?date?components?to?represent?the?number?of?days?to?subtract?from?the?current?date.????The?weekday?value?for?Sunday?in?the?Gregorian?calendar?is?1,?so?subtract?1?from?the?number?of?days?to?subtract?from?the?date?in?question.??(If?today?is?Sunday,?subtract?0?days.)??*/????NSDateComponents?*componentsToSubtract?=?[[NSDateComponents?alloc]?init];????[componentsToSubtract?setDay:?0?-?([weekdayComponents?weekday]?-?1)];????NSDate?*beginningOfWeek?=?[gregorian?dateByAddingComponents:componentsToSubtract?toDate:today?options:0];????/*??Optional?step:??beginningOfWeek?now?has?the?same?hour,?minute,?and?second?as?the?original?date?(today).????To?normalize?to?midnight,?extract?the?year,?month,?and?day?components?and?create?a?new?date?from?those?components.??*/????NSDateComponents?*components?=?[gregorian?components:(NSYearCalendarUnit?|?NSMonthCalendarUnit?|?NSDayCalendarUnit)?fromDate:?beginningOfWeek];????beginningOfWeek?=?[gregorian?dateFromComponents:components];?? ????????如何可以計算出一周的第一天(根據(jù)系統(tǒng)的日歷設置):
?
[cpp]?view plaincopy
NSDate?*today?=?[[NSDate?alloc]?init];????NSDate?*beginningOfWeek?=?nil;????BOOL?ok?=?[gregorian?rangeOfUnit:NSWeekCalendarUnit?startDate:&beginningOfWeek?interval:NULL?forDate:?today];??
??????? 獲得兩個日期之間的間隔:
?
?
[cpp]?view plaincopy
NSDate?*startDate?=?...;??NSDate?*endDate?=?...;????NSCalendar?*gregorian?=?[[NSCalendar?alloc]?initWithCalendarIdentifier:NSGregorianCalendar];????NSUInteger?unitFlags?=?NSMonthCalendarUnit?|?NSDayCalendarUnit;????NSDateComponents?*components?=?[gregorian?components:unitFlags?fromDate:startDate?toDate:endDate?options:0];????NSInteger?months?=?[components?month];??NSInteger?days?=?[components?day];?? ?
????????使用Category來計算同一時代(AD|BC)兩個日期午夜之間的天數(shù):
?
[plain]?view plaincopy
@implementation?NSCalendar?(MySpecialCalculations)????-(NSInteger)daysWithinEraFromDate:(NSDate?*)?startDate?toDate:(NSDate?*)?endDate?{???????NSInteger?startDay=[self?ordinalityOfUnit:NSDayCalendarUnit?inUnit:?NSEraCalendarUnit?forDate:startDate];?????????NSInteger?endDay=[self?ordinalityOfUnit:NSDayCalendarUnit?inUnit:?NSEraCalendarUnit?forDate:endDate];?????????return?endDay-startDay;??}????@end??
????????使用Category來計算不同時代(AD|BC)兩個日期的天數(shù):
?
?
[plain]?view plaincopy
@implementation?NSCalendar?(MyOtherMethod)????-(NSInteger)?daysFromDate:(NSDate?*)?startDate?toDate:(NSDate?*)?endDate?{?????????NSCalendarUnit?units=NSEraCalendarUnit?|?NSYearCalendarUnit?|?NSMonthCalendarUnit?|?NSDayCalendarUnit;?????????NSDateComponents?*comp1=[self?components:units?fromDate:startDate];???????NSDateComponents?*comp2=[self?components:units?fromDate?endDate];?????????[comp1?setHour:12];???????[comp2?setHour:12];?????????NSDate?*date1=[self?dateFromComponents:?comp1];???????NSDate?*date2=[self?dateFromComponents:?comp2];?????????return?[[self?components:NSDayCalendarUnit?fromDate:date1?toDate:date2?options:0]?day];??}????@end??
????????判斷一個日期是否在當前一周內(nèi)(使用格里高利歷):
?
?
[plain]?view plaincopy
-(BOOL)isDateThisWeek:(NSDate?*)date?{?????????NSDate?*start;???????NSTimeInterval?extends;?????????NSCalendar?*cal=[NSCalendar?autoupdatingCurrentCalendar];???????NSDate?*today=[NSDate?date];?????????BOOL?success=?[cal?rangeOfUnit:NSWeekCalendarUnit?startDate:&start?interval:?&extends?forDate:today];?????????if(!success)??????????return?NO;?????????NSTimeInterval?dateInSecs?=?[date?timeIntervalSinceReferenceDate];???????NSTimeInterval?dayStartInSecs=?[start?timeIntervalSinceReferenceDate];?????????if(dateInSecs?>?dayStartInSecs?&&?dateInSecs?<?(dayStartInSecs+extends)){????????????return?YES;???????}???????else?{????????????return?NO;???????}??}?
轉載于:https://www.cnblogs.com/geek6/p/4092794.html
總結
以上是生活随笔為你收集整理的iphone——日期处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。