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://stackoverflow.com/a/727536/1226226
<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
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(/^([az]{2,3})(?:-([az]{3})(?=$|-))?(?:-([az]{4})(?=$|-))?(?:-([az]{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'));
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'))
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.