Luxon js 从星期几中获取日期

Luxon js get day from week day number

Moment 可以选择根据周数(0 到 6)获取日期名称。我看到 luxon 有星期几,数字从 1 到 7(星期一是 1,星期日是 7)https://moment.github.io/luxon/#/parsing 但我不知道如何将星期数转换为日期名称?

马上

moment().day(0).format('ddddd'); // Sunday

我想看一下 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]);
})
.as-console-wrapper { max-height: 100% !important; top: 0; }
<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>