NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now;
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
| NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit
| NSSecondCalendarUnit;
now = [NSDate date];
comps = [calendar components:unitFlags fromDate:now];
week = [comps weekday];
month = [comps month];
day = [comps day];
hour = [comps hour];
min = [comps minute];
sec = [comps second];
unitFlags 是位元遮罩,用「或」把需要的單位組合起來。只要漏掉某個單位,對應屬性就會是 0,這是最常見的誤用。
- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate {
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; // 或 GMT
NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate *destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval
sinceDate:anyDate] autorelease];
return destinationDateNow;
}
原理是「兩時區各自與 GMT 的偏移量相減」,得到需要補上的秒數。以 UTC+8 來說,interval 會是 28800 秒。
- (NSDate *)stringToDate:(NSString *)strdate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *retdate = [dateFormatter dateFromString:strdate];
[dateFormatter release];
return retdate;
}
- (NSString *)dateToString:(NSDate *)date {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
return strDate;
}
NSDateFormatter 的建立成本不低,若要在 cellForRowAtIndexPath: 這類高頻路徑中大量使用,強烈建議把 formatter 做成靜態物件重用,效能差異相當明顯。
// 輸入格式範例:2013-08-03 12:53:51
- (NSString *)getUTCFormateLocalDate:(NSString *)localDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 輸入格式
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; // 輸出格式
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}
// 輸入的 UTC 格式範例:2013-08-03T04:53:51+0000
- (NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}
這裡有個容易忽略的細節:同一個 formatter 先後用了兩種格式,中間還改了 timeZone。NSDateFormatter 是有狀態的物件,重複使用時務必確認每個屬性都已重設,否則會出現「格式化結果莫名多出時區」這類難查的問題。
先說明幾個特殊符號:
(:) 時間分隔符。在某些區域設定中可能改用其他字元,實際顯示內容由 App 的當前 culture 決定。
(/) 日期分隔符。同上,用於分隔日、月、年。
(%) 表示無論後面接什麼字母,都以單字母格式讀取;也用於表示單字母格式應以使用者自訂格式讀取。
日期相關:
時間相關:
年與時區:
常見格式與輸出對照:
M/d/yy -> 12/7/58
d-MMM -> 7-Dec
d-MMMM-yy -> 7-December-58
d MMMM -> 7 December
MMMM yy -> December 58
hh:mm tt -> 08:50 PM
h:mm:ss t -> 8:50:35 P
H:mm -> 20:50
H:mm:ss -> 20:50:35
最後補充一個實務觀念:上表這套 % 前綴寫法源自 .NET 的自訂格式字串,而 NSDateFormatter 採用的是 Unicode TR35 的格式(yyyy 是年、YYYY 是週年、HH 是 24 小時、mm 是分、SS 是毫秒)。兩者語法不同,混用會得到看似正常卻在特定日期出錯的結果——最經典的就是用 YYYY 格式化 12 月底的日期,跨年那幾天會變成下一年。若專案同時涉及多個平台,務必分別對照各自的文件。