til

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.

Code snippet showing difference between US and GB

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:

MacOSX First day of week

I learnt this from @erikras