≪ Today I learned. RSS購読
公開日
更新日
タグ
JavaScript
著者
ダーシノ(@bc_rikko)

Intl.DurationFormatで時分秒の多言語対応をする

Chrome 129 BetaIntl.DurationFormatがサポートされた。現時点ではSafari 16.4のみサポート済み。

Intl.DurationFormatは国際化APIの一部で、時分秒の多言語対応とフォーマットができるようになる。

const duration = {
  years: 1,
  months: 2,
  weeks: 3,
  days: 4,
  hours: 5,
  minutes: 6,
  seconds: 7,
  milliseconds: 8,
  microseconds: 9,
  nanoseconds: 9,
};

new Intl.DurationFormat('ja-JP', {style: 'long'}).format(duration)
// 1 年 2 か月 3 週間 4 日 5 時間 6 分 7 秒 8 ミリ秒 9 マイクロ秒 9 ナノ秒

new Intl.DurationFormat('en-US', {style: 'long'}).format(duration)
// 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds, 8 milliseconds, 9 microseconds, 9 nanoseconds

new Intl.DurationFormat('en-US', {style: 'short'}).format(duration)
// 1 yr, 2 mths, 3 wks, 4 days, 5 hr, 6 min, 7 sec, 8 ms, 9 μs, 9 ns

new Intl.DurationFormat('en-US', {style: 'narrow'}).format(duration)
// 1y 2m 3w 4d 5h 6m 7s 8ms 9μs 9ns