0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DateFormatterのメモ

Posted at

###DateFormatterの出力について、Pointのメモ:
今日は日付の出力フォーマットについて、出来た経験をシェアします。
まず@rinov さんがやり方を教えてくれて、感謝します!
DateFormatだけ設定する場合

let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "GyMMM", options: 0, locale: Locale(identifier: "ja_JP")) //和暦のStyle
let dateLocale = formatter.string(from: Date())

#####結果は:AD2019年7月

DateFormatとlocalを設定する場合

let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "GyMMM", options: 0, locale: Locale(identifier: "ja_JP")) //和暦のStyleに設定
formatter.locale = Locale(identifier: "ja_JP")
let dateLocale = formatter.string(from: Date())

#####結果は:西暦2019年7月

DateFormatとlocalを設定する場合

let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "GyMMM", options: 0, locale: Locale(identifier: "ja_JP")) //和暦のStyleに設定
formatter.calendar = Calendar(identifier: .japanese)
let dateLocale = formatter.string(from: Date())

#####結果は:Reiwa1年7月
DateFormat、local、Calendarを設定する場合

let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "GyMMM", options: 0, locale: Locale(identifier: "ja_JP")) //和暦のStyleに設定
formatter.locale = Locale(identifier: "ja_JP")
formatter.calendar = Calendar(identifier: .japanese)
let dateLocale = formatter.string(from: Date()) 

#####結果は:令和元年7月

#####参考
https://qiita.com/rinov/items/bff12e9ea1251e895306

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?