iOS NSDateFormatter的NSLocale使用en_US_POSIX還是en_US

來源:互聯網
上載者:User

iOS SDK提供的日期時間格式化對象需要使用到NSLocale對象來控制日期和時間的顯示,en_US為標準的格式往往使用的最多,但是習慣使用Java或C#的朋友會忽略掉iOS SDK提供的另一種格式en_US_POSIX,並且兩種格式運行後的效果完全一樣。


參考代碼;

 代碼如下 複製代碼
NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
[formatter1 setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
NSDateFormatter *formatter2 = [[[NSDateFormatter alloc] init] autorelease];
[formatter2 setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
 
[formatter1 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
[formatter2 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
 
NSDate *date = [NSDate date];
NSLog(@"formatter1:%@",[formatter1 stringFromDate:date]);
NSLog(@"formatter2:%@",[formatter2 stringFromDate:date]);

到底使用en_US_POSIX還是en_US, Apple在SDK文檔中給出了答案.

 代碼如下 複製代碼

In most cases the best locale to choose is “en_US_POSIX”

因為

“en_US_POSIX” is also invariant in time (if the US, at some point in the future, changes the way it formats dates, “en_US” will change to reflect the new behaviour, but “en_US_POSIX” will not), and between machines (“en_US_POSIX” works the same on iPhone OS as it does on Mac OS X, and as it it does on other platforms).

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.