JavaScript, dealing with locale and hour cycle

Published in JavaScript on Sep 10, 2021

Finding current language locale and hour cycle (12 or 24 hours) from the client browser can be tricky in JavaScript, but it is possible...

To get current language locale you have to use:

let locale =  new Intl.DateTimeFormat().resolvedOptions().locale

And to get whether the language locale uses 12/24 hours you need to do:

let hourCycle = Intl.DateTimeFormat(locale, { hour: 'numeric' }).resolvedOptions().hourCycle

and hourCycle will return either "h11", "h12", "h23" or "h24".