简体   繁体   English

如何根据纯 Javascript 中的区域设置确定一周是从星期一还是星期日开始?

[英]How can I determine if week starts on Monday or Sunday based on locale in pure Javascript?

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.好吧,标题很好解释——我需要弄清楚当地的星期几开始——它可以是星期一、星期日、星期六或星期五——纯 Javascript。

I found this https://stackoverflow.com/a/727536/1226226我发现这个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找到了 ISO3166 和 ISO639 代码的兼容表 - 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.本质上是https://github.com/gamtiq/weekstart的重度高尔夫版本,忽略了一些不寻常的语言环境。 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.没有纯 JS 答案(使用截至 2020 年可用的 API)是完美的,但这将产生与系统 API 相同的结果,至少对 99.9% 的用户而言。

https://github.com/tc39/proposal-intl-locale-info has been proposed which will include an API level solution for this. https://github.com/tc39/proposal-intl-locale-info已经提出,其中将包含一个 API 级别的解决方案。

 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 .基于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.周日返回 0,周一返回 1,周六返回 -1,周五返回 -2,因此您可以使用它来制作日历。 Returns 1 for unknown country codes.未知国家代码返回 1。

{
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.js

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

As for pure js, you would have to make your own lookup table.至于纯 js,您必须制作自己的查找表。 You can use momentjs source code for reference.您可以使用 momentjs 源代码作为参考。 https://github.com/moment/moment/tree/develop/locale https://github.com/moment/moment/tree/develop/locale

In each of each locale file look for the dow at the end of the config.在每个语言环境文件中查找配置末尾的dow

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中获取从星期一到星期日开始的上周日期 - How can I get last week date starting monday to sunday in javascript 我如何获得本周一和周日之间的日期AngularJS - How I can get the date this week between Monday and Sunday AngularJS 请问如何在javascript中让工作日从星期一而不是星期日开始? - How to make the week days start at monday instead of sunday in javascript please? Javascript:获取前一周的周一和周日 - Javascript: get Monday and Sunday of the previous week 在 Javascript 中获取一周中的天数,一周从星期日开始 - Get the days in a Week in Javascript, Week starts on Sunday 如何在javascript中从当前日期起7天创建一个星期几的数组(即0表示星期日,1表示星期一等)? - How to create an array of numbers of the days of the week (i.e. 0 for sunday, 1 for monday, etc.) 7 days from current date in javascript? 在 javascript 日历上,月份从星期日开始,但周从星期一开始 - On a javascript calendar, the month starts on Sunday, but weeks start on monday 在 Javascript 中获取周一开始和周日结束的一周开始和结束 - Get start and end of week with the start on Monday and end on Sunday in Javascript 检查nodejs中的星期几是否在星期一和星期日之间 - Check if day of week is between Monday and sunday in nodejs 如何确定周日,如第一个星期日,第二个星期日,第三个星期日......通过约会 - How to determine week days like first sunday, second sunday, third sunday … by passing a date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM