I’m currently building a calendar data display for Atuin, and wanted to ensure that the week started with the correct day.
While I’m a big fan of things being configurable and flexible, I don’t want to introduce a new config option unless it’s required!
Luckily browser localisation exposes plenty of options.
Get a Locale object
First we need a Locale! They’re created like so
new Intl.Locale('CODE')
// eg
new Intl.Locale('en-GB')
We can get the code as follows
new Intl.Locale(navigator.language)
This is a set by the user in their browser
Get the weekinfo
Once we have the Locale, it’s simple
let locale = new Intl.Locale(navigator.language);
let weekinfo = locale.getWeekInfo();
console.log(weekinfo...
I’m currently building a calendar data display for Atuin, and wanted to ensure that the week started with the correct day.
While I’m a big fan of things being configurable and flexible, I don’t want to introduce a new config option unless it’s required!
Luckily browser localisation exposes plenty of options.
Get a Locale object
First we need a Locale! They’re created like so
new Intl.Locale('CODE')
// eg
new Intl.Locale('en-GB')
We can get the code as follows
new Intl.Locale(navigator.language)
This is a set by the user in their browser
Get the weekinfo
Once we have the Locale, it’s simple
let locale = new Intl.Locale(navigator.language);
let weekinfo = locale.getWeekInfo();
console.log(weekinfo.firstDay); // prints 1 for me, in the UK