javascriptluxon

Luxon js get day from week day number


Moment had the option to get the day name based on week number (0 to 6). I see that luxon has day of the week, as number from 1-7 (Monday is 1, Sunday is 7)https://moment.github.io/luxon/#/parsing but I dont know how can I convert the week number to day name?

In moment

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

Solution

  • I'd have a look at luxon's Info.weekdays, this will give day names in various formats.

    However the day numbering is slightly different to Moment, so you'll have to translate.

    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>