Moment 可以选择根据周数(0 到 6)获取日期名称。我看到 luxon 有星期几,从 1 到 7 的数字(星期一是 1,星期日是 7)https://moment.github.io/luxon/#/parsing但我不知道如何转换星期数字到日期名称?
瞬间
moment().day(0).format('ddddd'); // Sunday
Run Code Online (Sandbox Code Playgroud)
我会看一下 luxon 的Info.weekdays,这将给出各种格式的日期名称。
然而,日期编号与 Moment 略有不同,因此您必须进行翻译。
const { Info } = luxon;
// moment.js days of week: Sunday: 0 -> Monday: 6
const momentDays = [0,1,2,3,4,5,6];
console.log('Index', 'Long', 'Short');
momentDays.forEach(day => {
// Luxon day indexes are Monday: 0 -> Sunday 6
const luxonDay = (day + 6) % 7;
console.log(day, Info.weekdays('long')[luxonDay], Info.weekdays('short')[luxonDay]);
})Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.0.2/luxon.min.js" integrity="sha512-frUCURIeB0OKMPgmDEwT3rC4NH2a4gn06N3Iw6T1z0WfrQZd7gNfJFbHrNsZP38PVXOp6nUiFtBqVvmCj+ARhw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>Run Code Online (Sandbox Code Playgroud)