Objective-C學習篇10—NSDate與NSDateFormatter,objective-cnsdate
NSDate
NSDate 時間類,繼承自NSObject,其對象表示一個時間點
NSDate *date = [NSDate date]; NSLog(@"date = %@", date);
2015-12-04 19:08:00.624 OCNSDate[2955:309612] date = 2015-12-04 11:08:00 +0000
列印顯示的是格裡尼治時間 年-月-日 時:分:秒 + 時區
1. 得到一個距離目前時間間隔時間點的建立方法 dateWithTimeIntervalSinceNow:(NSTimeInterval)
(NSTimeInterval) 的本質是double資料類型
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8*60*60]; // 用格裡尼治時間加8個小時的時差,得到北京時間 NSLog(@"date1 = %@", date1);
2015-12-04 19:08:00.625 OCNSDate[2955:309612] date1 = 2015-12-04 19:08:00 +0000
練習1:得到明天當前的時間點 NSDate *nextDay = [NSDate dateWithTimeIntervalSinceNow:24*60*60 + 8*60*60]; 練習2:得到明年的時間點 NSDate *nextYear = [NSDate dateWithTimeIntervalSinceNow:366*24*60*60 + 8*60*60]; NSLog(@"%@", nextYear);
2. 計算給定時間和目前時間點的時間間隔 .timeIntervalSinceNow
NSTimeInterval interval = nextDay.timeIntervalSinceNow;
NSLog(@"%.2f", interval);
3. 計算兩個時間點的時間間隔 timeIntervalSinceDate:
NSTimeInterval interval2 = [date timeIntervalSinceDate:nextYear];
NSLog(@"%.2f", interval2);
時間戳記的概念: 一個時間點距離 1970.1.1 的時間間隔,這個時間是以秒為單位,就叫做時間戳記
時間戳記的求法: timeIntervalSince1970
NSTimeInterval interval3 = [date timeIntervalSince1970];
NSLog(@"%.2f", interval3);
將時間戳記轉化為時間對象 dateWithTimeIntervalSince1970:
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:3600];
NSLog(@"%@", date2);
擷取北京時間 dateByAddingTimeInterval:
NSDate *date3 = [date dateByAddingTimeInterval:8*60*60];
NSLog(@"%@", date3);
練習3: 一個目前時間和一個固定時間的差值,如果差值在60秒之內,則輸出"剛剛",如果時間差值在60~3600秒,則輸出在"xx分鐘之前", 如果在3600~24*3600之內,則輸出在"xx小時之前",如果在24*3600秒之外輸出固定的時間
固定時間 NSDate *pastDate = [NSDate dateWithTimeIntervalSinceNow:-370]; NSLog(@"%@", pastDate);
目前時間 NSDate *nowDate = [NSDate date]; 固定時間與目前時間的差值 NSTimeInterval interval4 = [nowDate timeIntervalSinceDate:pastDate]; NSLog(@"時間差為 %.2f 秒", interval4);
if (interval4 <= 60) { NSLog(@"剛剛"); }else if(interval4 <= 3600){ NSLog(@"%.f分鐘之前", interval4 / 60);
}else if(interval4 <= 24*3600){ NSLog(@"%.f小時之前", interval4 / 3600); }else if(interval4 > 24*3600){ NSLog(@"%@", pastDate); }
NSDateFormatter
NSDateFormatter 日期格式類,繼承自NSFormatter,主要作用是將NSDate對象轉換為某種格式,然後以字串的形式輸出
NSDateFormatter *formartter = [[NSDateFormatter alloc] init];
1. 設定日期格式中用到的字母: y 代表年, M 代表 月, d 代表天 H 代表時 m 代表分, s 代表秒
[formartter setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"]; NSDate *date4 = [NSDate dateWithTimeIntervalSinceNow:0];
2. 將時間對象轉為設定的格式 stringFromDate:
格式化的時候系統會自動加上距離零時區的時間間隔
NSString *dateString = [formartter stringFromDate:date4]; NSLog(@"%@", dateString);
練習: 將date4以@"2015年份09月份24號 11點43分20秒"的形式輸出
建立一個時間格式類對象 init方法建立
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
指定輸出格式 setDateFormat:@"具體時間格式"
[myFormatter setDateFormat:@"yyyy年份MM月份dd號 HH點mm分ss秒"];
用一個字串接收轉化後的時間 stringFromDate:
NSString *dateString1 = [myFormatter stringFromDate:date4];
NSLog(@"%@", dateString1);
將時間字串轉換為NSDate對象
例如: @"2015年11月24號 11時10分10秒" NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init]; [formatter3 setDateFormat:@"yyyy年MM月dd號 HH時mm分ss秒"]; 準備時間字串 NSString *dateString3 = @"2015年11月24號 11時10分10秒"; 使用時間格式對象藉助時間字串格式化時間對象 NSDate *date5 = [formatter3 dateFromString:dateString3];
轉過來的時間會被迴歸到零時區的時間 NSLog(@"%@", date5); 如果想得到北京時間需要手動加上8小時 NSDate *date6 = [date5 dateByAddingTimeInterval:8*60*60]; NSLog(@"%@", date6);