1

Is there a easy way to get all week day names sorted depending on locale?

Info.weekdays() seems to be pretty close to what I want, but the sorting is not how I would expect it.

Example:

const info = luxon.Info

console.log(info.weekdays('short', {locale: 'de'}));
// actual: ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']

console.log(info.weekdays('short', {locale: 'en-US'}));
// actual: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
// expected: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

Is there maybe a util function to get the start of week for a locale to resort this array or is there a more easy approach with the Intl API?

Thanks in advance!

kerosene
  • 930
  • 14
  • 31
  • If you want the week to start on sunday, just rotate the array you're gettig? – Bergi Jan 01 '23 at 15:05
  • as I understand, OP want information which weekday is assumed to be first based on locale(e.g. to render week calendar up to this information). I'd rather allow user to set it up to their preferences instead – skyboyer Jan 01 '23 at 17:03
  • yep, rotating is easy if you know which day is the starting point by locale. The question is going in the same direction as https://stackoverflow.com/questions/53382465. I agree that allowing users to set their preferences is good, but a good default based on their locale is necessary. – kerosene Jan 02 '23 at 07:09
  • I am also just realizing that the defaults for en-US and en-GB are different. See [cldr-json](https://github.com/unicode-org/cldr-json/blob/f27f55f1dd487af7cf4260f56296ee1c7649b7fc/cldr-json/cldr-core/supplemental/weekData.json). However, `console.log(info.weekdays('short', {locale: 'en-US'}));` does not bring a different result. For moment.js it just would have been `moment.localeData('en-us').firstDayOfWeek();` – kerosene Jan 02 '23 at 07:12

1 Answers1

1

Based on tickets in luxon's repo #373 and #939 which are about similar cases and up to discussion - everything was blocked by browser API was not providing such information.

Nowadays, TC39 proposal has already been shipped(source: MDN) to Chrome 99+, Safari 15.4+, Edge. Unfortunately, not to Firefox. Yet.

Looks like luxon's team did not revisited this feature so far, I don't see any reference to weekInfo in their code, conditional or not. So you probably better to implement your homebrew algo:

(new Intl.Locale('en-GB')).weekInfo.firstDay // 1
(new Intl.Locale('en-US')).weekInfo.firstDay // 7

and if weekInfo will not be available to FF, with fallback to either Sunday or Monday.

skyboyer
  • 22,209
  • 7
  • 57
  • 64