NSDateFormatter 代表一個日期格式器,它的功能就是完成NSDate 與 NSString 之間的轉換。NSDateFormatter 的setDateStyle、 setTimeStyle 方法可以設置格式化日期、時間的風格,其中,日期、時間風格支持如下幾個枚舉值:
NSDateFormatterNoStyle; //不顯示日期、時間風格NSDateFormatterShortStyle;// 顯示"短"的日期、時間風格NSDateFormatterMediumStyle;// 顯示"中等"的日期、時間風格NSDateFormatterLongStyle;// 顯示"完整"的日期、時間風格
NSDate *date = [NSDate dateWithTimeIntervalSince1970:3600 * 24 * 366 * 20]; //1970年1月1日開始,20年之后的日期
// 創(chuàng)建兩個NSLocele,分別代表中國、美國
NSLocale *locales[] = {[NSLocale localeWithLocaleIdentifier:@"zh_CN"], [NSLocale localeWithLocaleIdentifier:@"en_US"]};
NSDateFormatter *dateFormater[7]; // 創(chuàng)建一個NSDateFormatter 數(shù)組
for (int i = 0; i < 2; i++) {
// ----------1. ShortStyle 的日期、時間風格 || NSLocale----------
dateFormater[i * 4] = [[NSDateFormatter alloc] init];
[dateFormater[i * 4] setDateStyle:NSDateFormatterShortStyle];
[dateFormater[i * 4] setTimeStyle:NSDateFormatterShortStyle];
[dateFormater[i * 4] setLocale:locales[i]];
// 獲取從
// ----------2. MediumStyle 的日期、時間風格 || NSLocale----------
dateFormater[i * 4 + 1] = [[NSDateFormatter alloc] init];
[dateFormater[i * 4 + 1] setDateStyle:NSDateFormatterMediumStyle];
[dateFormater[i * 4 + 1] setTimeStyle:NSDateFormatterMediumStyle];
[dateFormater[i * 4 + 1] setLocale:locales[i]];
// ----------3. LongStyle 的日期、時間風格 || NSLocale----------
dateFormater[i * 4 + 2] = [[NSDateFormatter alloc] init];
[dateFormater[i * 4 + 2] setDateStyle:NSDateFormatterLongStyle];
[dateFormater[i * 4 + 2] setTimeStyle:NSDateFormatterLongStyle];
[dateFormater[i * 4 + 2] setLocale:locales[i]];
// ----------4. FullStyle 的日期、時間風格 || NSLocale----------
dateFormater[i * 4 + 3] = [[NSDateFormatter alloc] init];
[dateFormater[i * 4 + 3] setDateStyle:NSDateFormatterFullStyle];
[dateFormater[i * 4 + 3] setTimeStyle:NSDateFormatterFullStyle];
[dateFormater[i * 4 + 3] setLocale:locales[i]];
}
for (int i = 0; i < 2; i++) {
switch (i) {
case 0: NSLog(@"----------中國日期格式----------"); break;
case 1: NSLog(@"----------美國日期格式----------");
default: break;
}
//分別打印兩個地區(qū)的4種時間格式
NSLog(@"Short 格式日期:%@", [dateFormater[i * 4] stringFromDate:date]);
NSLog(@"Medium 格式日期:%@", [dateFormater[i * 4 + 1] stringFromDate:date]);
NSLog(@"Long 格式日期:%@", [dateFormater[i * 4 + 2] stringFromDate:date]);
NSLog(@"Full 格式日期:%@", [dateFormater[i * 4 + 3] stringFromDate:date]);
}
打印結果:
2016-06-18 23:35:39.382 時間相關[822:26449] ----------中國日期格式----------
2016-06-18 23:35:39.387 時間相關[822:26449] Short 格式日期:90/1/16 上午8:00
2016-06-18 23:35:39.387 時間相關[822:26449] Medium 格式日期:1990年1月16日 上午8:00:00
2016-06-18 23:35:39.390 時間相關[822:26449] Long 格式日期:1990年1月16日 GMT+8 上午8:00:00
2016-06-18 23:35:39.390 時間相關[822:26449] Full 格式日期:1990年1月16日 星期二 中國標準時間 上午8:00:00
2016-06-18 23:35:39.390 時間相關[822:26449] ----------美國日期格式----------
2016-06-18 23:35:39.391 時間相關[822:26449] Short 格式日期:1/16/90, 8:00 AM
2016-06-18 23:35:39.391 時間相關[822:26449] Medium 格式日期:Jan 16, 1990, 8:00:00 AM
2016-06-18 23:35:39.391 時間相關[822:26449] Long 格式日期:January 16, 1990 at 8:00:00 AM GMT+8
2016-06-18 23:35:39.392 時間相關[822:26449] Full 格式日期:Tuesday, January 16, 1990 at 8:00:00 AM China Standard Time
喜歡的話,幫忙點一下喜歡,謝謝!
如果有錯誤之處或者偏差,還請斧正!
歡迎大家留言提問,技術要交流才能更快成長!