javascript 使用 momentJs 根据 Locale 获取一周的第一天是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26658254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:17:14  来源:igfitidea点击:

Getting what is the first day of the week based on Locale with momentJs

javascriptmomentjs

提问by Mario Levrero

Using momentJs, is possible to get the first day of the week (Monday(1), Sunday(7)...) based on Locale without creating a new moment?

使用 momentJs,是否可以根据 Locale 获取一周的第一天(星期一(1)、星期日(7)...)而不创建新的时刻?

I know I can access what is the first day of the week for the current locale with:

我知道我可以通过以下方式访问当前语言环境的一周的第一天:

moment.locale('uk');
moment().startOf('week').isoWeekday(); //Returns 1
moment.locale('en');
moment().startOf('week').isoWeekday(); //Returns 7

But I think it's a bit ugly...

不过我觉得有点丑。。。

  1. Creating a momentjs object.
  2. Going to the first date of the week.
  3. Resolving the weekDay.
  1. 创建一个 momentjs 对象。
  2. 前往一周的第一个日期。
  3. 解决weekDay。

Any better idea? Thx!

有什么更好的主意吗?谢谢!

回答by motig88

This question has a proper answer in momentjs's current API:

这个问题在 momentjs 的当前 API 中有一个正确的答案:

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

As OP asked - no instance of moment() needed, also no ugliness of going to "start of", just plain simple use of their localeData.

正如 OP 所问的那样 - 不需要 moment() 的实例,也没有“开始”的丑陋,只是简单地使用他们的 localeData。

Note, that it might be required to download the moment+locale file which is significantly larger (44kb) than moment only (about 12kb).

请注意,可能需要下载比仅时刻(约 12kb)大得多的时刻+区域设置文件(44kb)。

Seems to be the case from version 2.2.0, more info can be found on their docs: http://momentjs.com/docs/#/i18n/locale-data/

似乎是 2.2.0 版的情况,可以在他们的文档中找到更多信息:http: //momentjs.com/docs/#/i18n/locale-data/

回答by durrrr

Looks to me that you wanted to get a locale aware start of week (startOf('week')) and return its value as an isoWeekday, not the date or anything? In that case your question is the answer.

在我看来,您想获得周开始的区域设置 ( startOf('week')) 并将其值作为 返回isoWeekday,而不是日期或其他任何内容?在这种情况下,您的问题就是答案。

moment().startOf('week').isoWeekday();

回答by AberZombie

It looks like you can just do moment().locale('us').weekday(0) as of version 2.1.0 http://momentjs.com/docs/#/get-set/weekday/

看起来你可以只做 moment().locale('us').weekday(0) 从版本 2.1.0 http://momentjs.com/docs/#/get-set/weekday/