iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐰

Reference Table for DateFormatter's setLocalizedDateFormatFromTemplate() Formats and Outputs

に公開

Using DateFormatter's setLocalizedDateFormatFromTemplate() is convenient as it allows you to obtain localized date-related strings. Below is a summary of the correspondence between the formats used and the resulting output.

var formatter = DateFormatter()
formatter.calendar = .current
//formatter.calendar = Calendar(identifier: .japanese)
formatter.locale = .current
//formatter.locale = Locale(identifier: "ja_JP")

// Specify the format
formatter.setLocalizedDateFormatFromTemplate("y MMM d E")

// -> en_US: "Wed, Feb 4, 2026"
// -> ja_JP: "2026年2月4日(水)"
let dateString = formatter.string(from: Date())

Format

"y MMM d E"

  • The order of symbols does not affect the output order
  • Unrecognized characters and spaces are ignored
  • Output results vary depending on the Locale
  • Output results may differ between single and combined uses (see composite patterns)

Year (Gregorian Calendar)

Format en_US ja_JP
y 2026 2026年
yy 26 26年
yyy 2026 2026年
yyyy 2026 2026年
yyyyy 02026 02026年

(The number of leading zeros increases with the number of 'y's)

Year (Japanese Calendar)

Format en_US ja_JP Notes
y 31 31年 Jan 7, 1989 – Apr 30, 2019
yyy 031 31年 Jan 7, 1989 – Apr 30, 2019
y 1 元年 May 1, 2019 onwards
yy 01 元年 May 1, 2019 onwards
Gy 31 Heisei 平成31年 Jan 7, 1989 – Apr 30, 2019
Gyyy 031 Heisei 平成31年 Jan 7, 1989 – Apr 30, 2019
Gy 1 Reiwa 令和元年 May 1, 2019 onwards
Gyy 01 Reiwa 令和元年 May 1, 2019 onwards

(The number of leading zeros increases with the number of 'y's, but the number of digits doesn't change in Japanese. In Japanese, 1 is represented as "元".)

Month

Format en_US ja_JP
M 2 2月
MM 02 02月
MMM Feb 2月
MMMM February 2月
MMMMM F 2月
MMMMMM 000002 2月
MMMMMMM 0000002 2月

(The number of leading zeros increases with the number of 'M's, but the number of digits doesn't change in Japanese.)

Day

Format en_US ja_JP
d 4 4日
dd 04 04日
ddd 004 004日

(The number of leading zeros increases with the number of 'd's)

Day of the Week

Format en_US ja_JP
E Wed
EE Wed
EEE Wed
EEEE Wednesday 水曜日
EEEEE W
EEEEEE We
EEEEEEE Wed

Hour

Format en_US ja_JP
H 09 9時

(In these languages, the number of digits does not change.)

Format en_US ja_JP
h 9 AM 午前9時

(In these languages, the number of digits does not change.)

Minute

Format en_US ja_JP
m 09 8

(In these languages, the number of digits does not change, and no units are added.)

Second

Format en_US ja_JP
s 7 7

(In these languages, the number of digits does not change, and no units are added.)

Era (Gregorian Calendar)

Format en_US ja_JP
G AD 西暦
GGGGG A AD

Era (Japanese Calendar)

Format en_US ja_JP
G Reiwa 令和
GGGGG R R

(Is it impossible to get notations like "令", "平", or "昭" in Japanese?)

Composite Patterns

Output results change when multiple format characters are combined, compared to using them individually.

Format en_US ja_JP
yM 2/2026 2026/2
yMd 2/4/2026 2026/2/4
yMdE Wed, 2/4/2026 2026/2/4(水)
yMdEEEE Wednesday, 2/4/2026 2026/2/4水曜日
yyMMdd 02/04/26 26/02/04
Hm 09:08 9:08
hm 9:08 AM 午前9:08
Hms 09:08:07 09:08:07
y MMM d H m s Feb 4, 2026 at 09:08:07 2026年2月4日 9:08:07

Discussion