LoginSignup
4
4

More than 5 years have passed since last update.

ユーザの24時間表示の設定を取得する

Posted at
let _24 = (NSDateFormatter.dateFormatFromTemplate("HH", options: 0, locale: .currentLocale())!.rangeOfString("a") == nil)

つらい。
どうやら NSLocale.currentLocale() には24時間表記かどうかの情報が含まれているっぽいのだが、APIが生えていない。

日本語設定の時に、12時間表示か24時間表示かを取得して、
16:324:32 PM を切り替えたいだけなのに。
16:32午後4:32 なら簡単なのになぁ。

NSDateFormatter.dateFormatFromTemplate("HH:mm", options: 0, locale: .currentLocale()) で取っちゃうと、
"aK:mm"になるから、PM4:32になっちゃう。

結局こう。

let locale = NSLocale(localeIdentifier: "en_US")
formatter.locale = locale
let _24 = (NSDateFormatter.dateFormatFromTemplate("HH", options: 0, locale: .currentLocale())!.rangeOfString("a") == nil)
formatter.dateFormat = _24 ? "H:mm" : "K:mm a"

なんかかっこ悪いなぁ。

4
4
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
4
4