How to get the first day of week
javascript
Today I found a tweet from Erikras mentioning this.
Date.prototype.getDay() returns a value 0-6, with 0 being the 🇺🇸-centric first day of week: Sunday. But
Intl.Locale.prototype.weekInfo.firstDay
is from 1-7, with 1=Monday, 7=Sunday.
So the thing is that a function to get the first day of week could look like this:
function isFirstDayOfWeek(date: Date): boolean {
const locale = new Intl.Locale(navigator.language);
const firstDayOfWeek = locale.weekInfo.firstDay;
const dayOfWeek = date.getDay();
return dayOfWeek === firstDayOfWeek % 7;
}
There not seems to be a way of getting the first day of week defined by the system:
I learnt this from @erikras