您立即可以致电:
momentLocaleData.firstDayOfWeek()
Luxon 是否有可能获得相同的功能?
更新答案
Luxon 在版本中添加了本地化周支持3.4.4
。现在你可以使用startOf https://moment.github.io/luxon/api-docs/index.html#datetimestartof using useLocaleWeeks
option:
opts.useLocaleWeeks
boolean
(默认false
)
如果为 true,则根据区域设置使用周,即使用与区域设置相关的一周开始时间
Example:
// Luxon
const DateTime = luxon.DateTime;
console.log( DateTime.local().setLocale('fr-CA').startOf('week', {useLocaleWeeks: true}).toISO() );
// Moment.js
console.log( moment().locale('fr-ca').startOf('week').format() );
<script src="https://cdn.jsdelivr.net/npm/[email protected] /cdn-cgi/l/email-protection/build/global/luxon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment-with-locales.min.js"></script>
之前版本的旧答案3.4.4
:
我担心,目前(当前版本2.4.0
),使用 Luxon 不可能实现这一点。
勒克森 1.xdocs https://github.com/moment/luxon/blob/1.28.0/docs/matrix.md#effects-of-missing-features states:
基本国际化。Luxon 的代码中没有国际化字符串;相反,它依赖于 Intl API 的主机实现。这包括非常方便的到区域设置字符串 https://moment.github.io/luxon/api-docs/index.html#datetimetolocalestring。大多数浏览器和最新版本的 Node 都支持这一点。
而版本 2.x 的参考位于Intl https://moment.github.io/luxon/#/intl section
此外,使用 Luxon,您总是将星期一作为一周的第一天,如以下代码片段所示:
// Luxon
const DateTime = luxon.DateTime;
console.log( DateTime.local().setLocale('fr-CA').startOf('week').toISO() );
// Moment.js
console.log( moment().locale('fr-ca').startOf('week').format() );
<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment-with-locales.min.js"></script>
请注意,即使有'week'
参数输入startOf https://moment.github.io/luxon/api-docs/index.html#datetimestartof文档中,它始终返回星期一作为一周的第一天。
将此 DateTime“设置”为时间单位的开始。
startOf(unit: string): DateTime
参数
unit (string)
要转到开头的单元。可以是“年”、“季度”、“月”、“周”、“日”、“小时”、“分钟”、“秒”或“毫秒”。
Example
DateTime.local(2014, 3, 3).startOf('week').toISODate(); //=> '2014-03-03', weeks always start on Mondays
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)