一篇文章吃透iOS、JS的時間日期(Date, Calendar, Locale, TimeZone)
阿新 • • 發佈:2018-05-12
內容 dig fun sta target 格式 特定 ID lin
iOS
時間相關類
- NSDate - 表示一個絕對的時間點。
- NSCalendar - 代表一個特定的日歷,例如公歷或者希伯來日歷。它提供了一系列基於日期的計算,並且可以讓你在"NSDate"和"NSDateComponents"對象之間進行轉換。
- NSDateComponents - 允許你獲取一個Date的特定內容,例如小時、分鐘、年月日等等。
- NSTimeZone - 代表一個特定的時區信息,可以幫助跨時區的計算任務。
代碼分析
廢話少說,Show me the code
/**
* 日歷
*/
//公歷
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *date = [NSDate new];
NSLog(@"%ld-%ld-%ld",
[calendar component:NSCalendarUnitYear fromDate:date],
[calendar component:NSCalendarUnitMonth fromDate:date],
[calendar component:NSCalendarUnitDay fromDate:date]);
// 公歷:2018-5-9
//佛歷
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierBuddhist];
NSLog(@"%ld-%ld-%ld",
[calendar component:NSCalendarUnitYear fromDate:date],
[calendar component:NSCalendarUnitMonth fromDate:date],
[calendar component:NSCalendarUnitDay fromDate:date]);
// 佛歷:2561-5-9
//日本日歷
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese];
NSLog(@"%ld-%ld-%ld",
[calendar component:NSCalendarUnitYear fromDate:date],
[calendar component:NSCalendarUnitMonth fromDate:date],
[calendar component:NSCalendarUnitDay fromDate:date]);
// 日本日歷:30-5-9
/**
* 地區
*/
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"Default Locale Formatted Date:%@", formattedDateString);
// 系統為公歷:Default Locale Formatted Date:9 May 2018 at 4:25:06 PM GMT+8
// 系統為佛歷:Default Locale Formatted Date:9 May 2561 BE at 4:21:29 PM GMT+8
//中國Locale
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"ZH Locale Formatted Date:%@", formattedDateString);
// ZH Locale Formatted Date:2018年5月9日 GMT+8 下午4:21:29
/**
* 時區
*/
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"GMT Timezone Formatted Date:%@", formattedDateString);
// GMT Timezone Formatted Date:2018年5月9日 GMT 上午8:21:29
/**
* NSDateComponents
*/
// Yearless date
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMonth:11];
[components setDay:7];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *birthday = [gregorian dateFromComponents:components];
formattedDateString = [dateFormatter stringFromDate:birthday];
NSLog(@"GMT Timezone Formatted Date:%@", formattedDateString);
// GMT Timezone Formatted Date:1年11月6日 GMT 下午3:54:17
JavaScript
關於JavaScript的Date對象可以參考以下鏈接:
Understanding Date and Time in JavaScript
JavaScript Date Objects
獲取Date屬性
const birthday = new Date(1980, 6, 31);
birthday.getFullYear(); // 1980
birthday.getMonth(); // 6
birthday.getDate(); // 31
birthday.getDay(); // 4
birthday.getHours(); // 0
birthday.getMinutes(); // 0
birthday.getSeconds(); // 0
birthday.getMilliseconds(); // 0
birthday.getTime(); // 333849600000 (for GMT)
Date格式化
var options = { weekday: ‘long‘, year: ‘numeric‘, month: ‘long‘, day: ‘numeric‘ };
var today = new Date();
today.toLocaleDateString("en-US"); // 5/9/2018
today.toLocaleDateString("en-US",options); // Wednesday, May 9, 2018
today.toLocaleDateString("hi-IN", options); // ??????, 9 ?? 2018
Moment.js
Moment.js -Parse, validate, manipulate, and display dates and times in JavaScript。Moment是一個非常強大的JavaScript時間日期庫,是對原生對象的很好的擴展。
//Format
moment().format(‘MMMM Do YYYY, h:mm:ss a‘); // May 9th 2018, 8:05:15 pm
//Calendar Time
moment().add(10, ‘days‘).calendar();
//Multiple Locale Support
moment.locale(); // en
//TimeZone
var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z");
jun.tz(‘America/Los_Angeles‘).format(‘ha z‘); // 5am PDT
dec.tz(‘America/Los_Angeles‘).format(‘ha z‘); // 4am PST
目前還有一個更新、更現代的時間日期庫:luxon,可以嘗試一下。
一篇文章吃透iOS、JS的時間日期(Date, Calendar, Locale, TimeZone)