#3373·Carbon

Support moment.js era tokens (N…NNNNN, y, yo) in isoFormat() and createFromIsoFormat()

Author: kamthornCreated Sep 30, 2026Updated Sep 30, 2026

Proposal

isoFormat() follows moment.js tokens, but it lacks moment's era support (format tokens, locale eras; added in moment 2.25.0, #4599). Without it, there's no way to output a year in a non-Christian era, such as the Thai Buddhist Era (2026 CE = 2569 BE) or Japanese eras, and no way to parse one back.

Same semantics and names as moment:

Token Meaning th en (default)
N, NN, NNN era abbreviation พ.ศ. AD
NNNN era name พุทธศักราช Anno Domini
NNNNN era narrow name พ.ศ. AD
y year of era 2569 2026
yo ordinal year of era 2569 2026th

Eras would live in locale files with moment's fields (since, until, offset, name, narrow, abbr), with AD/BC in en as the fallback for every locale. Era year = (year − since.year) × direction + offset. That's pure arithmetic, needs no ext-intl, and doesn't change any date calculation.

Parsing (createFromIsoFormat() / createFromLocaleIsoFormat()) would convert the era year to CE in the input string before calling createFromFormat(). Parsing it as a CE year first and then subtracting would break Feb 29: createFromFormat('!Y-m-d', '2567-02-29') gives 2567-03-01.

How this differs from earlier requests

This has come up before:

  • #53 (2013) added a Buddhist-era flag to formatLocalized(). It was declined as too specific.
  • #624: "one date library should handle one calendar".
  • #2954 (2024): addYears(543) turned 2024-02-29 into 2567-03-01. A formatBuddhist() macro was later added to the docs.

This proposal is different in three ways:

  1. No new calendar. The Thai solar calendar has the same days and months as the Gregorian calendar (since 1941, when the Thai new year moved to January 1). Only the year label differs, like Japanese eras. The date stays a normal Gregorian DateTime, and nothing in date arithmetic changes.
  2. Not Thai-specific. It's moment.js's generic era mechanism, which isoFormat() is modeled on. Thai and Japanese would just be two locales with eras data.
  3. Parsing works too. A macro can format, but it can't help createFromIsoFormat() avoid the #2954 pitfall.

Questions

  1. Backward compatibility: unescaped N and y in user formats are currently printed literally. Would adding them as tokens be acceptable in a minor release (moment has had them since 2.25.0, May 2020), or should it wait for a major? Locale data is being cleaned of stray y in #3371, so shipped formats won't change meaning.
  2. Should there be an opt-in way for th to use the Buddhist Era in L/LL, or should the defaults stay CE and users write D MMMM N y explicitly? I'd keep the defaults unchanged.
  3. Is era support in translatedFormat() (PHP date() letters) of interest? PHP has no free letter for it, so it would need a separate design. I'd leave it out for now.

If this sounds acceptable, I'd send it as two PRs: formatting first, then parsing.