luxon

How to create momentLocaleData.firstDayOfWeek() in luxon?


In moment you can call:

momentLocaleData.firstDayOfWeek()

Is it possible to get the same functionality in Luxon?


Solution

  • Updated answer

    Luxon has added Localized week support in version 3.4.4. Now you can use startOf using useLocaleWeeks option:

    opts.useLocaleWeeks boolean (default false)

    If true, use weeks based on the locale, i.e. use the locale-dependent start of the week

    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/luxon@3.4.4/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>


    Old answer for version before 3.4.4:

    I fear that, at the moment (current version 2.4.0), it is not possible achieve this using Luxon.

    The Luxon 1.x docs states:

    Basic internationalization. Luxon doesn't have internationalized strings in its code; instead it relies on the hosts implementation of the Intl API. This includes the very handy toLocaleString. Most browsers and recent versions of Node support this.

    while refence for version 2.x is in Intl section

    Moreover, using Luxon, you always get Monday as the first day of the week, as you can see in the following snippet:

    // 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>

    Please note that, even if there is 'week' parameter in startOf docs, it returns always Monday as first day of the week.

    "Set" this DateTime to the beginning of a unit of time.

    startOf(unit: string): DateTime
    

    Parameters

    unit (string) The unit to go to the beginning of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'.

    Example

    DateTime.local(2014, 3, 3).startOf('week').toISODate(); //=> '2014-03-03', weeks always start on Mondays