之前一篇文章寫的是時間與日期處理的簡單模式函筋,這篇文章就算是拓展的模式吧走净。
上一篇文章的地址是——IOS開發(fā)之時間與日期處理(簡單)
本文主要涉及的是:
NSTimeZone -- 時區(qū)信息
NSLocale -- 本地化信息
NSDateComponents -- 一個封裝了具體年月日、時秒分哟冬、周秸苗、季度等的類
NSCalendar -- 日歷類,它提供了大部分的日期計算接口派继,并且允許您在NSDate和NSDateComponents之間轉(zhuǎn)換
NSTimeZone介紹
NSTimeZone表示時區(qū)信息宾袜。 有下面幾種初始化方法:
1. + (id)timeZoneWithName:(NSString *)aTimeZoneName / - (id)initWithName:(NSString *)aName
根據(jù)時區(qū)名稱初始化捻艳。可以調(diào)用NSTimeZone的類方法 + (NSArray *)knownTimeZoneNames來返回所有已知的時區(qū)名稱庆猫。
NSTimeZone *zone = [[NSTimeZone alloc] initWithName:@"America/Chicago"];
//NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"America/Chicago"];
NSLog(@"%@",zone);
打印出:America/Chicago (CST) offset -21600
*2. + (id)timeZoneWithAbbreviation:(NSString )abbreviation
根據(jù)時區(qū)縮寫初始化认轨。例如:EST(美國東部標準時間)、HKT(香港標準時間)
NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSLog(@"%@",zone);
打印出:Asia/Hong_Kong (HKT) offset 28800
*3. + (NSTimeZone )systemTimeZone
返回系統(tǒng)時區(qū)
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"%@",zone);
假如時區(qū)是上海月培,打印出的時區(qū)信息將會是:Asia/Shanghai (CST (China)) offset 28800嘁字,28800代表相對于GMT時間偏移的秒數(shù),即8個小時杉畜。(8*60*60)
*4. + (NSTimeZone )localTimeZone
返回本地時區(qū)纪蜒,與systemTimeZone的區(qū)別在于:本地時區(qū)可以被修改,而系統(tǒng)時區(qū)不能修改此叠。
[NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@"America/Chicago"]];
NSTimeZone *systemZone = [NSTimeZone systemTimeZone];
NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSLog(@"%@",systemZone);
NSLog(@"%@",localZone);
打印出的系統(tǒng)時區(qū)仍然是:Asia/Shanghai (CST (China)) offset 28800纯续;而本地時區(qū)經(jīng)過修改后,變成了:Local Time Zone (America/Chicago (CST) offset -21600)
5. + (id)timeZoneForSecondsFromGMT:(NSInteger)seconds
根據(jù)零時區(qū)的秒數(shù)偏移返回一個新時區(qū)對象
NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:28800];
NSLog(@"%@",zone);
打印出:GMT+0800 (GMT+08:00) offset 28800
NSTimeZone常用對象方法與類方法:
*1. + (NSArray )knownTimeZoneNames
以數(shù)組的形式返回所有已知的時區(qū)名稱
NSArray *zoneArray = [NSTimeZone knownTimeZoneNames];
for(NSString *str in zoneArray){
NSLog(@"%@",str);
}
2. - (NSString *)name / - (NSString *)abbreviation
返回時區(qū)對象的名稱或縮寫
NSTimeZone *zone = [NSTimeZone localTimeZone];
NSString *strZoneName = [zone name];
NSString *strZoneAbbreviation = [zone abbreviation];
NSLog(@"name is %@",strZoneName);
NSLog(@"abbreviation is %@",strZoneAbbreviation);
打印結(jié)果為 name is Asia/Hong_Kong
abbreviation is HKT
3. - (NSInteger)secondsFromGMT
得到當前時區(qū)與零時區(qū)的間隔秒數(shù)
NSTimeZone *zone = [NSTimeZone localTimeZone];
int seconds = [zone secondsFromGMT];
NSLog(@"%i",seconds);
NSLoale介紹
NSLoale類返回本地化信息灭袁,主要體現(xiàn)在"語言"和"區(qū)域格式"這兩個設(shè)置項猬错。有下面幾種初始化方法:
1. + (id)systemLocale
返回系統(tǒng)初始本地化信息
NSLocale *locale = [NSLocale systemLocale];
NSLog(@"%@",[[locale objectForKey:NSLocaleCalendar] calendarIdentifier]);
2. + (id)currentLocale / + (id)autoupdatingCurrentLocale
這兩個類方法都將返回當前客戶端的本地化信息,區(qū)別在于:currentLocale取得的值會一直保持在cache中茸歧,第一次用此方法實例化對象后倦炒,即使修改了本地化設(shè)定,這個對象也不會改變软瞎。而使用autoupdatingCurrentLocale逢唤,當每次修改本地化設(shè)定拉讯,其實例化的對象也會隨之改變。
下面的代碼演示了區(qū)別所在智玻,假設(shè)初始本地化信息為en_US遂唧,先用這兩個函數(shù)分別初始化兩個對象,然后修改本地化設(shè)定語言為臺灣繁體中文吊奢,再重新打印這兩個對象的信息:
NSLocale *locale1;
NSLocale *locale2;
- (IBAction)doTest:(id)sender {
locale1 = [NSLocale currentLocale];
locale2 = [NSLocale autoupdatingCurrentLocale];
NSLog(@"%@",locale1.localeIdentifier);
//print "en_US" NSLog(@"%@",locale2.localeIdentifier);
//print "en_US"
}
- (IBAction)doAgain:(id)sender {
NSLog(@"%@",locale1.localeIdentifier);
//print "en_US" NSLog(@"%@",locale2.localeIdentifier);
//print "zh_TW"
}
*3. - (id)initWithLocaleIdentifier:(NSString )string
用標示符初始化本地化信息
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
NSString *strSymbol = [locale objectForKey:NSLocaleCurrencySymbol];
NSLog(@"%@",strSymbol);
代碼用"zh_CN"來初始化對象盖彭,然后再打印出對象的貨幣符號,得到的結(jié)果是人民幣符號¥
NSLoale常用對象方法與類方法:
1. - (id)objectForKey:(id)key
根據(jù)不同的key返回各種本地化信息页滚,例如下面的代碼返回了當前貨幣符號:
NSLocale *locale = [NSLocale currentLocale];
NSString *strSymbol = [locale objectForKey:NSLocaleCurrencySymbol];
NSCalendar *calendar = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar];
*2. - (NSString )displayNameForKey:(id)key value:(id)value
顯示特定地區(qū)代號下相應(yīng)鍵的顯示名稱:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
NSString *str = [locale displayNameForKey:NSLocaleIdentifier value:@"en_US"];
NSLog(@"%@",str);
第一句代碼代表以中文來實例化對象召边,然后得到"en_US"的NSLocaleIdentifier鍵的顯示名稱。最后輸出的結(jié)果是"英文(美國)"
NSDateComponents介紹
NSDateComponents封裝了具體年月日裹驰、時秒分隧熙、周、季度等
NSDateComponents *compt = [[NSDateComponents alloc] init];[compt setEra:1];
[compt setYear:2013];
[compt setMonth:3];
[compt setDay:15];
[compt setHour:11];
[compt setMinute:20];
[compt setSecond:55];
[compt setQuarter:2];
[compt setTimeZone:[NSTimeZone systemTimeZone]];
[compt setWeek:3];
[compt setWeekday:4];
[compt setWeekOfMonth:3];
[compt setWeekOfYear:2];
[compt setCalendar:[NSCalendar currentCalendar]];
NSDateComponents相關(guān)方法:
1. - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date
取得一個NSDate對象的1個或多個部分幻林,用NSDateComponents來封裝
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
//NSDateComponents *compt = [calendar components:NSDayCalendarUnit fromDate:date];
NSDateComponents *compt = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];
NSLog(@"%d,%@",[compt year],date);
NSLog(@"%d,%@",[compt month],date);
NSLog(@"%d,%@",[compt day],date);
需要注意的是贞盯,只有明確指定了unitFlags,NSDateComponents相應(yīng)的那一部分才有值沪饺。
2. - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options : (NSUInteger)opts
取得兩個NSDate對象的時間間隔躏敢,用NSDateComponents來封裝
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
NSDate *date2 = [NSDate dateWithTimeInterval:5*60*60+75 sinceDate:date];
NSDateComponents *compt = [calendar components:(NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date toDate:date2 options:0];
NSLog(@"%d",[compt minute]);
NSLog(@"%d",[compt second]);
有幾點需要注意:
① 得到的NSDateComponents對象可能會包含負數(shù)。例如:當toDate比fromDate晚10秒整葡,second部分返回10件余;當toDate比fromDate早10秒,second部分返回-10
② 當指定unitFlags返回多個部分時遭居,相隔的時間由多個部分共同組成(而不是獨立去表示)啼器。例如:上面的例子時間相差5小時1分15秒,如果指定只返回second部分俱萍,將得到18075秒端壳;如果指定返回minute和second部分,將得到301分15秒枪蘑;如果指定返回hour损谦、minute和second,將得到5小時1分15秒腥寇。
3. - (NSDate *)dateFromComponents:(NSDateComponents *)comps
根據(jù)NSDateComponents對象得到一個NSDate對象
NSDateComponents *compt = [[NSDateComponents alloc] init];
[compt setYear:2012];
[compt setMonth:5];
[compt setDay:11];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [calendar dateFromComponents:compt];
//得到本地時間成翩,避免時區(qū)問題
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];
NSDate *localeDate = [date dateByAddingTimeInterval:interval];
NSLog(@"%@",localeDate);
4. - (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSUInteger)opts
在參數(shù)date基礎(chǔ)上,增加一個NSDateComponents類型的時間增量
NSDateComponents *compt = [[NSDateComponents alloc] init];
[compt setDay:25];
[compt setHour:4];
[compt setMinute:66];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [calendar dateByAddingComponents:compt toDate:[NSDate date] options:0];
//得到本地時間赦役,避免時區(qū)問題
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];
NSDate *localeDate = [date dateByAddingTimeInterval:interval];
NSLog(@"%@",localeDate);
當前時間的基礎(chǔ)上麻敌,增加25天4小時66秒
NSCalendar介紹
這是一個日歷類,可以處理日歷的相關(guān)問題
1. + (id)currentCalendar / + (id)autoupdatingCurrentCalendar
這兩個類方法都將返回當前客戶端的邏輯日歷掂摔,區(qū)別在于:currentCalendar取得的值會一直保持在cache中术羔,第一次用此方法實例化對象后赢赊,即使修改了系統(tǒng)日歷設(shè)定,這個對象也不會改變级历。而使用autoupdatingCurrentCalendar释移,當每次修改系統(tǒng)日歷設(shè)定,其實例化的對象也會隨之改變寥殖。
下面的代碼演示了區(qū)別所在玩讳,假設(shè)初始Calendar設(shè)定為NSGregorianCalendar(公歷),先用這兩個函數(shù)分別初始化兩個對象嚼贡,然后修改系統(tǒng)日歷為NSJapaneseCalendar(日本和歷)熏纯,再重新打印這兩個對象的信息:
NSCalendar *calendar;
NSCalendar *calendar2;
- (IBAction)doTest:(id)sender {
calendar = [NSCalendar currentCalendar]; calendar2 = [NSCalendar autoupdatingCurrentCalendar];
NSLog(@"%@",calendar.calendarIdentifier);
//print "gregorian" NSLog(@"%@",calendar2.calendarIdentifier);
//print "gregorian"
}
- (IBAction)doAgain:(id)sender {
NSLog(@"%@",calendar.calendarIdentifier);
//print "gregorian"
NSLog(@"%@",calendar2.calendarIdentifier);
//print "japanese"
}
*2. - (id)initWithCalendarIdentifier:(NSString )string
根據(jù)提供的日歷標示符初始化
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
NSLog(@"%@",calendar.calendarIdentifier);
系統(tǒng)中定義的日歷有:
NSGregorianCalendar -- 公歷(常用)
NSBuddhistCalendar -- 佛教日歷
NSChineseCalendar -- 中國農(nóng)歷(常用)
NSHebrewCalendar -- 希伯來日歷
NSIslamicCalendar -- 伊斯蘭歷
NSIslamicCivilCalendar -- 伊斯蘭教日歷
NSJapaneseCalendar -- 日本日歷(和歷,常用)
NSRepublicOfChinaCalendar -- 中華民國日歷(臺灣)
NSPersianCalendar -- 波斯歷
NSIndianCalendar -- 印度日歷
NSISO8601Calendar -- ISO8601(但是現(xiàn)在還不可用)
NSCalendar常用對象方法與類方法:
*1. - (void)setLocale:(NSLocale )locale
設(shè)置本地化信息
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
NSLog(@"%@",calendar.locale.localeIdentifier);
*2. - (void)setTimeZone:(NSTimeZone )tz
設(shè)置時區(qū)信息
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"HKT"]];
NSLog(@"%@",calendar.timeZone);
3. - (void)setFirstWeekday:(NSUInteger)weekday
設(shè)置每周的第一天從星期幾開始粤策,比如:1代表星期日開始樟澜,2代表星期一開始,以此類推叮盘。默認值是1
如圖所示秩贰,如果從星期天開始,日歷的表現(xiàn)形式:
如果從星期二開始柔吼,日歷的表現(xiàn)形式:
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:3];
NSLog(@"%i",calendar.firstWeekday);
4. - (void)setMinimumDaysInFirstWeek:(NSUInteger)mdw
設(shè)置每年及每月第一周必須包含的最少天數(shù)毒费,比如:設(shè)定第一周最少包括3天,則value傳入3
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setMinimumDaysInFirstWeek:3];
NSLog(@"%i",calendar.minimumDaysInFirstWeek);
5. - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
獲取一個小的單位在一個大的單位里面的序數(shù)
NSCalendarUnit包含的值有:
NSEraCalendarUnit -- 紀元單位嚷堡。對于NSGregorianCalendar(公歷)來說蝗罗,只有公元前(BC)和公元(AD)艇棕;而對于其它歷法可能有很多蝌戒,例如日本和歷是以每一代君王統(tǒng)治來做計算。
NSYearCalendarUnit -- 年單位沼琉。值很大北苟,相當于經(jīng)歷了多少年,未來多少年打瘪。
NSMonthCalendarUnit -- 月單位友鼻。范圍為1-12NSDayCalendarUnit -- 天單位。范圍為1-31
NSHourCalendarUnit -- 小時單位闺骚。范圍為0-24
NSMinuteCalendarUnit -- 分鐘單位彩扔。范圍為0-60
NSSecondCalendarUnit -- 秒單位。范圍為0-60
NSWeekCalendarUnit -- 周單位僻爽。范圍為1-53
NSWeekdayCalendarUnit -- 星期單位虫碉,每周的7天。范圍為1-7
NSWeekdayOrdinalCalendarUnit -- 沒完全搞清楚
NSQuarterCalendarUnit -- 幾刻鐘胸梆,也就是15分鐘敦捧。范圍為1-4
NSWeekOfMonthCalendarUnit -- 月包含的周數(shù)须板。最多為6個周
NSWeekOfYearCalendarUnit -- 年包含的周數(shù)。最多為53個周
NSYearForWeekOfYearCalendarUnit -- 沒完全搞清楚
NSTimeZoneCalendarUnit -- 沒完全搞清楚
下面是一些示例:
① 當小單位為NSWeekdayCalendarUnit兢卵,大單位為NSWeekCalendarUnit時(即某個日期在這一周是第幾天)习瑰,根據(jù)firstWeekday屬性不同,返回的結(jié)果也不同秽荤。
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:10];
//[calendar setFirstWeekday:2];
int count = [calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:date];
NSLog(@"%d",count);
默認firstWeekday為1(星期天開始)的情況下甜奄,得到的結(jié)果是2,從下圖可以看到是第2天窃款。
假如firstWeekday被設(shè)置為2(星期一開始)的情況下贺嫂,得到的結(jié)果是1,從下圖可以看到是第1天
② 當小單位為NSWeekCalendarUnit雁乡,大單位為NSYearCalendarUnit時(即某個日期在這一年中是第幾周)第喳,根據(jù)minimumDaysInFirstWeek屬性不同,返回的結(jié)果也不同踱稍。
NSDateComponents *compt = [[NSDateComponents alloc] init];
[compt setYear:2013];
[compt setMonth:1];
[compt setDay:20];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [calendar dateFromComponents:compt];
//[calendar setMinimumDaysInFirstWeek:6];
int count = [calendar ordinalityOfUnit:NSWeekCalendarUnit inUnit:NSYearCalendarUnit forDate:date];
NSLog(@"%d",count);
從上圖的日歷中可以看出曲饱,在沒有設(shè)置minimumDaysInFirstWeek的情況下,1月20日得到的結(jié)果是4(第四個周)珠月。
默認情況下第一個周有5天扩淀,如果將minimumDaysInFirstWeek設(shè)置為6天,則原本是第一周的1月1日--1月5日被劃分到了上一年啤挎,返回0驻谆;而1月6日--1月12日升為第一周,1月13日--1月19日升為第二周庆聘,依此類推胜臊。所以需要關(guān)注的是minimumDaysInFirstWeek與實際第一周包含天數(shù)的大小比較,如果提供的minimumDaysInFirstWeek比實際第一周的天數(shù)小伙判,則一切不變象对;否則統(tǒng)計"一年中第幾周"、"一個月中第幾周"的時候會產(chǎn)生變化宴抚。
6. -(NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
根據(jù)參數(shù)提供的時間點勒魔,得到一個小的單位在一個大的單位里面的取值范圍
NSDateComponents *compt = [[NSDateComponents alloc] init];
[compt setYear:2013];
[compt setMonth:2];
[compt setDay:21];
[compt setHour:9];
[compt setMinute:45];
[compt setSecond:30];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [calendar dateFromComponents:compt];
//得到本地時間,避免時區(qū)問題
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];
NSDate *localeDate = [date dateByAddingTimeInterval:interval];
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:localeDate];
NSLog(@"%d -- %d",range.location,range.length);
調(diào)用這個方法要明確一點菇曲,取得的是"范圍"而不是"包含"冠绢,下面是一些例子:
① 小單位是NSDayCalendarUnit,大單位是NSYearCalendarUnit常潮,并不是要取這一年包含多少天弟胀,而是要取"天"(Day)這個單位在這一年(Year)的取值范圍。其實不管你提供的日期是多少,返回的值都是"1--31"邮利。
② 小單位是NSDayCalendarUnit弥雹,大單位是NSMonthCalendarUnit。要取得參數(shù)時間點所對應(yīng)的月份下延届,"天"(Day)的取值范圍剪勿。根據(jù)參數(shù)時間的月份不同,值也不同方庭。例如2月是1--28厕吉、3月是1--31、4月是1--30械念。
③ 小單位是NSWeekCalendarUnit头朱,大單位是NSMonthCalendarUnit。要取得參數(shù)時間點所對應(yīng)的月份下龄减,"周"(Week)的取值范圍项钮。需要注意的是結(jié)果會受到minimumDaysInFirstWeek屬性的影響。在默認minimumDaysInFirstWeek情況下希停,取得的范圍值一般是"1--5"烁巫,從日歷上可以看出來這個月包含5排,即5個周宠能。
④ 小單位是NSDayCalendarUnit亚隙,大單位是NSWeekCalendarUnit。要取得周所包含的"天"(Day)的取值范圍违崇。下面是一個示例日歷圖:
在上圖的日期條件下阿弃,假如提供的參數(shù)是4月1日--4月6日,那么對應(yīng)的week就是1(第一個周)羞延,可以看到第一個周包含有6天渣淳,從1號開始,那么最終得到的范圍值為1--6肴楷。
假如提供的參數(shù)是4月18日水由,那么對應(yīng)的week是3(第三個周)荠呐,第三個周包含有7天赛蔫,從14號開始,那么最終得到的范圍值是14--7泥张。
假如提供的參數(shù)是4月30日呵恢,那么對應(yīng)的week是5(第五個周),第五個周只包含3天媚创,從28號開始渗钉,那么最終得到的范圍值是28--3。
7. - (BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate *)datep interval:(NSTimeInterval *)tip forDate:(NSDate *)date
根據(jù)參數(shù)提供的時間點,返回所在日歷單位的開始時間鳄橘。如果startDate和interval均可以計算声离,則返回YES;否則返回NO
unit -- 日歷單位datep -- 開始時間瘫怜,通過參數(shù)返回tip -- 日歷單位所對應(yīng)的秒數(shù)术徊,通過參數(shù)返回date -- 時間點參數(shù)
NSDate *dateOut = nil;
NSTimeInterval count = 0;
NSDateComponents *compt = [[NSDateComponents alloc] init];
[compt setYear:2013];
[compt setMonth:3];
[compt setDay:22];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [calendar dateFromComponents:compt];
BOOL b = [calendar rangeOfUnit:NSMonthCalendarUnit startDate:&dateOut interval:&count forDate:date];
if(b){
//得到本地時間,避免時區(qū)問題
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:dateOut];
NSDate *localeDate = [dateOut dateByAddingTimeInterval:interval];
NSLog(@"%@",localeDate);
NSLog(@"%f",count);
}else{
NSLog(@"無法計算");
}
上面的例子要求返回2013年3月22日當月的起始時間鲸湃,以及當月的秒數(shù)赠涮。得到的結(jié)果是:2013-03-01 00:00:00 +0000,2678400暗挑。(2678400 = 31天 * 24小時 * 60分 * 60秒)
假如將上面的日歷單位改為NSWeekCalendarUnit笋除,那么得到的結(jié)果是:2013-03-17 00:00:00 +0000,604800炸裆。當周的第一天是3月17日垃它。(604800 = 7天 * 24小時 * 60分 * 60秒)
假如將上面的日歷單位改為NSYearCalendarUnit,那么得到的結(jié)果是:2013-01-01 00:00:00 +0000烹看,31536000嗤瞎。這一年的第一天是1月1日。(31536000 = 365天 * 24小時 * 60分 * 60秒)
關(guān)于NSCalendar 的具體用法也可以參考——Cocoa Foundation框架學(xué)習(xí)筆記 - NSCalendar里邊有具體的代碼實現(xiàn)听系。
關(guān)于時間與日期的問題暫且寫到這里贝奇。
本文算是拓展的內(nèi)容,一般用不到這么深入靠胜,在上一篇簡單模式中的東西差不多就夠用了掉瞳。
說實話,復(fù)制粘貼也是很累的啊——wayne23
目前寫(copy)的東西浪漠,還有以后寫(copy)的東西陕习,基本都是為了自己能夠找的更方便而已,純粹是懶得址愿,莫笑该镣。