時(shí)間格式和時(shí)區(qū)

/*

G:? ? ? ? 公元時(shí)代,例如AD公元

yy:? ? 年的后2位

yyyy:? ? 完整年

MM:? ? 月,顯示為1-12,帶前置0

MMM:? ? 月,顯示為英文月份簡(jiǎn)寫,如 Jan

MMMM:? ? 月,顯示為英文月份全稱,如 Janualy

dd:? ? 日,2位數(shù)表示,如02

d:? ? ? ? 日,1-2位顯示,如2,無(wú)前置0

EEE:? ? 簡(jiǎn)寫星期幾,如Sun

EEEE:? ? 全寫星期幾,如Sunday

aa:? ? 上下午,AM/PM

H:? ? ? ? 時(shí),24小時(shí)制,0-23

HH:? ? 時(shí),24小時(shí)制,帶前置0

h:? ? ? ? 時(shí),12小時(shí)制,無(wú)前置0

hh:? ? 時(shí),12小時(shí)制,帶前置0

m:? ? ? ? 分,1-2位

mm:? ? 分,2位,帶前置0

s:? ? ? ? 秒,1-2位

ss:? ? 秒,2位,帶前置0

S:? ? ? ? 毫秒

Z:? ? ? ? GMT(時(shí)區(qū))

//任何NSTimeZone對(duì)象所代表的時(shí)區(qū)都是相對(duì)于GMT的, iOS中的時(shí)間類NSDate所獲取到的時(shí)間, 都是相對(duì)于GMT的.

1、NSTimeZone 時(shí)區(qū)的創(chuàng)建

NSTimeZone *zone1 = [[NSTimeZone alloc] init];

// 根據(jù)時(shí)區(qū)名稱創(chuàng)建

NSTimeZone *zone2 = [[NSTimeZone alloc] initWithName:@"America/Chicago"];

NSTimeZone *zone3 = [NSTimeZone timeZoneWithName:@"America/Chicago"];

// 根據(jù)時(shí)區(qū)縮寫創(chuàng)建

NSTimeZone *zone4 = [NSTimeZone timeZoneWithAbbreviation:@"EST"];

// 根據(jù)零時(shí)區(qū)的秒數(shù)偏移量創(chuàng)建

NSTimeZone *zone5 = [NSTimeZone timeZoneForSecondsFromGMT:28800];

2、NSTimeZone 時(shí)區(qū)的設(shè)置

// 設(shè)置默認(rèn)的時(shí)區(qū)

[NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@"America/Chicago"]];

NSTimeZone *systemZone = [NSTimeZone systemTimeZone];

// 本地時(shí)區(qū)可以被修改,而系統(tǒng)時(shí)區(qū)不能修改。

NSTimeZone *localZone = [NSTimeZone localTimeZone];

NSTimeZone *defaultZone = [NSTimeZone defaultTimeZone];

// NSCalendar 設(shè)置時(shí)區(qū)

//設(shè)置時(shí)區(qū),設(shè)置為 GMT+8,即北京時(shí)間(+8)

NSCalendar *calendar = [NSCalendar currentCalendar];

[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CET"]];

[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+28800]];

// NSDateFormatter 設(shè)置時(shí)區(qū)

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// 設(shè)置時(shí)區(qū),設(shè)置為 GMT

[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

3、NSTimeZone 時(shí)區(qū)的獲取

// 獲取系統(tǒng)時(shí)區(qū)

NSTimeZone *zone1 = [NSTimeZone systemTimeZone];

// 獲取本地時(shí)區(qū)

NSTimeZone *zone2 = [NSTimeZone localTimeZone];

// 獲取默認(rèn)時(shí)區(qū)

NSTimeZone *zone3 = [NSTimeZone defaultTimeZone];

4、獲取時(shí)區(qū)的名稱

// 獲取所有已知的時(shí)區(qū)名稱

NSArray *zoneNames = [NSTimeZone knownTimeZoneNames];

NSTimeZone *zone = [NSTimeZone localTimeZone];

// 獲取指定時(shí)區(qū)的名稱

NSString *strZoneName = [zone name];

5、獲取時(shí)區(qū)的縮寫

// 獲取所有已知的時(shí)區(qū)縮寫

NSDictionary *zoneAbbreviations = [NSTimeZone abbreviationDictionary];

NSTimeZone *zone = [NSTimeZone localTimeZone];

// 獲取指定時(shí)區(qū)的縮寫

NSString *zoneAbbreviation1 = [zone abbreviation];

// 獲取指定時(shí)間所在時(shí)區(qū)名稱縮寫

NSString *zoneAbbreviation2 = [zone abbreviationForDate:[NSDate date]];

6、獲取與零時(shí)區(qū)的間隔秒數(shù)

NSTimeZone *zone = [NSTimeZone localTimeZone];

// 獲取當(dāng)前時(shí)區(qū)與零時(shí)區(qū)的間隔秒數(shù)

NSInteger seconds1 = [zone secondsFromGMT];

// 獲取指定時(shí)間所在時(shí)區(qū)與零時(shí)區(qū)的間隔秒數(shù)

NSInteger seconds2 = [zone secondsFromGMTForDate:[NSDate date]];

7、時(shí)區(qū)差值轉(zhuǎn)換

// 得到當(dāng)前時(shí)間(世界標(biāo)準(zhǔn)時(shí)間 UTC/GMT)

NSDate *date = [NSDate date];

// 設(shè)置系統(tǒng)時(shí)區(qū)為本地時(shí)區(qū)

NSTimeZone *zone = [NSTimeZone systemTimeZone];

// 計(jì)算本地時(shí)區(qū)與 GMT 時(shí)區(qū)的時(shí)間差

NSInteger interval = [zone secondsFromGMT];

// 在 GMT 時(shí)間基礎(chǔ)上追加時(shí)間差值,得到本地時(shí)間

date = [date dateByAddingTimeInterval:interval];

*/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 像小強(qiáng)一樣活著閱讀 3,735評(píng)論 6 16
  • 主要有以下幾個(gè)類: NSDate:表示一個(gè)具體的絕對(duì)的時(shí)間點(diǎn)。NSTimeZone:表示時(shí)區(qū)信息。NSLocale...
    獻(xiàn)國(guó)閱讀 5,428評(píng)論 0 4
  • 在iOS開發(fā)中,經(jīng)常會(huì)遇到各種各樣的時(shí)間問(wèn)題,8小時(shí)時(shí)差,時(shí)間戳,求時(shí)間間隔,農(nóng)歷等等。解決辦法網(wǎng)上比比皆是,但大...
    真巧了_嘿閱讀 2,923評(píng)論 0 7
  • iOS開發(fā)中,經(jīng)常會(huì)遇到各種各樣的時(shí)間問(wèn)題,8小時(shí)時(shí)差,時(shí)間戳,求時(shí)間間隔,農(nóng)歷等等。解決辦法網(wǎng)上比比皆是,但大多...
    小李龍彪閱讀 6,741評(píng)論 1 6
  • 我享受調(diào)色時(shí)顏色一次又一次地變化!一次又一次地嘗試!從不敢到大膽下筆、從不知道怎么畫到仔細(xì)觀察!就在看似老...
    原生家庭的痛閱讀 166評(píng)論 0 0

友情鏈接更多精彩內(nèi)容