How to create momentLocaleData.firstDayOfWeek() in luxon?
Asked Answered
A

1

8

In moment you can call:

momentLocaleData.firstDayOfWeek()

Is it possible to get the same functionality in Luxon?

Ario answered 15/5, 2018 at 12:22 Comment(1)
I fear that it is not possible since docs states that: Luxon doesn't have internationalized strings in its code; instead it relies on the hosts implementation of the Intl API and I always get monday as first day of the week (see DateTime.local().setLocale('fr-CA').startOf('week').toISO() vs moment().locale('fr-ca').startOf('week').format()) and there is no week parameter in startOf docs.Villeneuve
V
7

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/[email protected]/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
Villeneuve answered 6/8, 2018 at 14:12 Comment(2)
Has been added in the meantime: The unit to go to the beginning of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'. - weeks always start on MondaysHaruspex
@WernfriedDomscheit you are right, week has been added to startOf docs, but unfortunately the functionality asked in the question is still not available since it returns always Monday as first day of the week.Villeneuve

© 2022 - 2024 — McMap. All rights reserved.