1
3

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.

Swiftの曜日表記について

Last updated at Posted at 2019-06-09

Swiftで曜日の文字列で取得する際に、希望する表記方法にするためにはどうすればいいのだろうと少し考えたので自分用にメモ。

// 現在の曜日を3文字で返す(Sun, Mon, Thu etc...)関数
private func getDayOfWeek() -> String{
    let formatter = DateFormatter()
    formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "EEE", options: 0, locale: Locale(identifier: "ja_JP"))
    return formatter.string(from: Date())
}

3文字にするためにはCalendarクラスを使うしかないかなー もしくは EEEEEした文字列.prefix(3)するしかないのかなーと思いましたが、よく探したらありました。

dateFormatのテンプレート対応表は以下になります。

fromTemplate 表記
EEE Sun
EEEE S
EEEEE Sunday

曜日以外の表記方法一覧はここにあります。
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

1
3
1

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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?