luxon
luxon copied to clipboard
Add optional startOfWeek param for startOf() and endOf() methods
In our app, the start day of week is a configurable property of an entity in our data model. When working with data related to that entity, we show calendar and schedule data relative to that start day of week regardless of locale settings.
I'd like to specify the weekday number that I consider the first day of the week when calling DateTime.startOf('week') or DateTime.endOf('week') similar to the existing useLocaleWeeks option.
Alternatives I've consider: implement this myself outside of Luxon :)
Example:
const jan19 = DateTime.fromISO('2024-01-19T12:34:56.789Z')
// Imagine the configured start of week is Saturday
jan19.startOf('week', { startOfWeek: 6 }).toISODate()
// "2024-01-13"
jan19.endOf('week', { startOfWeek: 6 }).toISODate()
// "2024-01-19"
Does Settings.defaultWeekSettings cover your use case?
Does
Settings.defaultWeekSettingscover your use case?
Sort of, but only if the user is always working in the context of a single instance of that entity that has startDayOfWeek, which is generally true in my app. But then I'm relying on that assumption, and I'd have to keep re-configuring that setting as the user moves around within the app. It'd be easier if I could specify at the time I need a startOf('week') or an endOf('week') that I think weeks start on Saturdays, or Mondays, or whatever.
I don't know your application, but to me this sounds like you're not really talking about "weeks". The concept of "week" doesn't change depending on what you are working on. In the en-US locale, a week always starts on a Sunday.
If the user works with different locales, you can still have separate DateTime instances with separate locales. If you generally just want "the last Saturday", that's not really "start of the week", it's just something you'll have to implement.
Although Luxon could theoretically provide a lastWeekday(X) and nextWeekday(X) API.
Maybe my use case is too specific and not something that Luxon should support. It just seems like a small specialization of the current behavior (use this exact value as startDayOfWeek instead of reading it from a setting).
To give a bit more context, one of the main features of the app is managing employee work schedules, which are 1 to 6 weeks long. So, we are definitely dealing in real weeks and real calendar concepts. Each schedule exists within an entity that has (among other things) a configurable start day of week.
Although Luxon could theoretically provide a lastWeekday(X) and nextWeekday(X) API.
I thought about this too, but the semantics are a little different in some cases. For example if today is Monday and startDayOfWeek is Monday, then I expect startOf('week') to give me today's date. Many people would reasonably expect giveMeTheLast(Monday) to give the prior Monday, not today's date, whereas startOf isn't ambiguous like that.
My only concern is that startOf is not the only place where startOfWeek is used. So, if we were to add this option, it would have to be added everywhere where startOfWeek is used. Some of those places are just getters (DateTime#localeWeekYear for example), so it would have to be done completely differently in that case.
What could be done is to expose more fine-grained locale configuration, i.e. provide a way to create a DateTime with locale en-US, but override the locale's startOfWeek for that DateTime only. In that case I can imagine other locale options being overridable as well.
Our app requires the same kind of behavior. Currently I keep switching between settings which looks like this:
const oldSettings = {...Settings.defaultWeekSettings};
const newSettings = weekStartsOnToInt(weekStartsOn);
Settings.defaultWeekSettings = newSettings;
const result = KeepingDateTime.fromObject(date.toObject()).startOf('week', {useLocaleWeeks: true});
Settings.defaultWeekSettings = {...newSettings, ...oldSettings};
return result;
Having the option to define the week settings on a DateTime basis would be cool. Especially if this could also be set with the DateTime.reconfigure() method.