How can I determine if week starts on Monday or Sunday based on locale in pure Javascript?
Asked Answered
V

5

9

Well, the title is pretty explanatory - I need to figure out the day week start in local - it can be Monday, Sunday, Saturday or Friday - in pure Javascript.

I found this https://mcmap.net/q/162310/-how-do-i-get-the-first-day-of-the-week-for-the-current-locale-php-l8n

<firstDay day="mon" territories="001 AD AI AL AM AN AT AX AZ BA BE BG BM BN BY CH CL CM CR CY CZ DE DK EC EE ES FI FJ FO FR GB GE GF GP GR HR HU IS IT KG KZ LB LI LK LT LU LV MC MD ME MK MN MQ MY NL NO PL PT RE RO RS RU SE SI SK SM TJ TM TR UA UY UZ VA VN XK" />
<firstDay day="fri" territories="BD MV" />
<firstDay day="sat" territories="AE AF BH DJ DZ EG IQ IR JO KW LY MA OM QA SD SY" />
<firstDay day="sun" territories="AG AR AS AU BR BS BT BW BZ CA CN CO DM DO ET GT GU HK HN ID IE IL IN JM JP KE KH KR LA MH MM MO MT MX MZ NI NP NZ PA PE PH PK PR PY SA SG SV TH TN TT TW UM US VE VI WS YE ZA ZW" />
<firstDay day="sun" territories="GB" alt="variant" references="Shorter Oxford Dictionary (5th edition, 2002)" />

found a compatible table of ISO3166 and ISO639 codes - https://wiki.openstreetmap.org/wiki/Nominatim/Country_Codes

Villegas answered 19/11, 2018 at 20:52 Comment(7)
#5210876Lavine
Possible duplicate of How to get first and last day of the week in JavaScriptMichaud
@Michaud - nope, this code shows that the first day of the week is Sunday, but in my current locale it should be MondayVillegas
Fitting that answer to your specific need is trivial.Michaud
@Michaud - not so trivial to me - there are countries where weeks start from Monday, Sunday, Saturday or even FridayVillegas
The first day of the week is not necessarily based on any particular rule. A person might prefer a particular day for administrative, religious or personal reasons, and might use a different day depending on context. I think the only way to get it right is to ask. Language (I guess NavigatorLanguage.language) is unreliable and locale (i.e. actual geographic location, not language) is not much better, but it may be more reliable where administrative rules are concerned.Although
I'm not aware of any way that does not rely on an external lookup table. But I think even if the first day of the week based on language-country might not always be correct, it is certainly better than to just assume "Sunday" like many services do.Nora
B
9

Without moment. Essentially a heavily golfed version of https://github.com/gamtiq/weekstart, ignoring some unusual locales. No pure JS answer (using APIs available as of 2020) is going to be perfect, but this will yield the same result as a system API would for at least 99.9% of users.

https://github.com/tc39/proposal-intl-locale-info has been proposed which will include an API level solution for this.

function weekStart(region, language) {
  const regionSat = 'AEAFBHDJDZEGIQIRJOKWLYOMQASDSY'.match(/../g);
  const regionSun = 'AGARASAUBDBRBSBTBWBZCACNCODMDOETGTGUHKHNIDILINJMJPKEKHKRLAMHMMMOMTMXMZNINPPAPEPHPKPRPTPYSASGSVTHTTTWUMUSVEVIWSYEZAZW'.match(/../g);
  const languageSat = ['ar','arq','arz','fa'];
  const languageSun = 'amasbndzengnguhehiidjajvkmknkolomhmlmrmtmyneomorpapssdsmsnsutatethtnurzhzu'.match(/../g);

  return (
    region ? (
      regionSun.includes(region) ? 'sun' :
      regionSat.includes(region) ? 'sat' : 'mon') : (
      languageSun.includes(language) ? 'sun' :
      languageSat.includes(language) ? 'sat' : 'mon'));
}

function weekStartLocale(locale) {
  const parts = locale.match(/^([a-z]{2,3})(?:-([a-z]{3})(?=$|-))?(?:-([a-z]{4})(?=$|-))?(?:-([a-z]{2}|\d{3})(?=$|-))?/i);
  return weekStart(parts[4], parts[1]);
}

console.log(weekStartLocale(navigator.language));
console.log(weekStartLocale('en'));
console.log(weekStartLocale('en-GB'));
console.log(weekStartLocale('ar-AE'));
Berlin answered 18/7, 2019 at 21:31 Comment(8)
Impressive golfing for something that usually implies loading a lot of data. I’m curious how complete this is and whether you think it could be generated?Deandreadeane
@Deandreadeane I lost the link to the JSON database I derived this from. github.com/gamtiq/weekstart is a more complete solution now; I may golf this one and post the results here.Berlin
Thanks. I’d add that the regexp for language tags here isn’t quite right — there won’t be underscores, for example — though perhaps these differences (which would rarely matter in practice) are addressing known issues in some agents. This is what I’d ended up using, derived from your answer: gist.github.com/bathos/84286d0d6052d10dd98a444d41e9db43 Thanks again — I’ll check out that link!Deandreadeane
@Deandreadeane no user agent should be sending an underscore as the separator: tools.ietf.org/html/bcp47Berlin
Yes, I agree. I must have stated that ambiguously, sorry. What I meant was that the RegExp pattern above is allowing for underscore as separator, which afaik isn’t necessary (though I wasn’t sure if maybe there was some known agent divergence being accounted for).Deandreadeane
D'oh. You are right. I got the regex from somewhere else. This was all the result of scavenging.Berlin
If it’s useful: the pattern in the gist linked to above should adhere to tools.ietf.org/html/rfc5646#section-2.1 aside from omiting grandfathered tags, numeric region tags, and ignoring everything after region.Deandreadeane
@Deandreadeane finally got around to golfing the better answer.Berlin
K
6

As of lately you can use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/weekInfo

It is available in all browsers (except firefox: March 2023)

code looks like this:

new Intl.Locale(navigator.language).weekInfo.firstDay // 1 or 7 ...

you can check for any locale by changing navigator.language with the desired language ex:

new Intl.Locale('fr').weekInfo.firstDay // 1
Krieger answered 25/3, 2023 at 6:59 Comment(1)
This didn't work for me because my language is different from my date.Ene
C
1

A more maintainable and usable version based on CLDR.

Based on country code. Returns 0 for Sunday, 1 for Monday, -1 for Saturday and -2 for Friday so you can use it to make a calendar. Returns 1 for unknown country codes.

{
let wso = { // fri:1, sat:2, sun:3
    MV:1,
    AE:2,AF:2,BH:2,DJ:2,DZ:2,EG:2,IQ:2,IR:2,JO:2,KW:2,LY:2,OM:2,QA:2,SD:2,SY:2,
    AG:3,AS:3,AU:3,BD:3,BR:3,BS:3,BT:3,BW:3,BZ:3,CA:3,CN:3,CO:3,DM:3,DO:3,ET:3,
    GT:3,GU:3,HK:3,HN:3,ID:3,IL:3,IN:3,JM:3,JP:3,KE:3,KH:3,KR:3,LA:3,MH:3,MM:3,
    MO:3,MT:3,MX:3,MZ:3,NI:3,NP:3,PA:3,PE:3,PH:3,PK:3,PR:3,PT:3,PY:3,SA:3,SG:3,
    SV:3,TH:3,TT:3,TW:3,UM:3,US:3,VE:3,VI:3,WS:3,YE:3,ZA:3,ZW:3,
}
function week_start_offset(country) {
    return (wso[country] || 4) - 3
}
}

console.log(week_start_offset('US'))
console.log(week_start_offset('RO'))
Carlock answered 16/2, 2022 at 9:46 Comment(0)
E
1

Following on from @user1928596

Their suggestion almost worked, but because my browser language is different to my date settings. I ended up using this.

  // Default to en-GB which is Monday -> Sunday
  const dateLocale = Intl
    ? Intl?.DateTimeFormat()?.resolvedOptions()?.locale
    : "en-GB"

  // @ts-ignore .weekInfo exists on all browsers except Firefox.
  const weekFirstDay = new Intl.Locale(dateLocale)?.weekInfo?.firstDay ?? 1
Ene answered 6/3 at 5:55 Comment(0)
S
-3

you could use moment.js

moment.localeData('en-us').firstDayOfWeek();

As for pure js, you would have to make your own lookup table. You can use momentjs source code for reference. https://github.com/moment/moment/tree/develop/locale

In each of each locale file look for the dow at the end of the config.

Sublimity answered 19/11, 2018 at 20:57 Comment(3)
I wish I could, but I can't use any 3rd party librariesVillegas
There is no built in way. Your best luck is to make a mapping of locales to firstDayOfTheWeek. It would be possible to make a lookup table for every locale.Sublimity
@Matt—the language is a very unreliable way of getting the user's locale or their preferred first day of the week.Although

© 2022 - 2024 — McMap. All rights reserved.