How do I obtain the preferred hourCycle
of the user’s current locale? I’ve tried a few locales ((new Intl.Locale('en-US')).hourCycle
) in both Firefox 131 and Chrome 129, but both return undefined
.
console.log(new Intl.Locale('en-US').hourCycle);
2
Answers
you can manually determine the hourCycle by formatting a date and checking the output:
The output from the 12-hour formatter includes "AM" or "PM," otherwise, it defaults to 24-hour.
Intl.Locale()
only returns this information if it’s passed via the locale tag or thehourCycle
option. To get thehourCycle
even if neither of these apply, you need to useIntl.DateTimeFormat().resolvedOptions()
instead (note that you need to specify thetimeStyle
option):