Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional startOfWeek param for startOf() and endOf() methods #1573

Open
jasonmobley opened this issue Jan 19, 2024 · 7 comments
Open

Add optional startOfWeek param for startOf() and endOf() methods #1573

jasonmobley opened this issue Jan 19, 2024 · 7 comments

Comments

@jasonmobley
Copy link

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"
@diesieben07
Copy link
Collaborator

Does Settings.defaultWeekSettings cover your use case?

@jasonmobley
Copy link
Author

Does Settings.defaultWeekSettings cover 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.

@diesieben07
Copy link
Collaborator

diesieben07 commented Jan 19, 2024

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.

@jasonmobley
Copy link
Author

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.

@jasonmobley
Copy link
Author

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.

@diesieben07
Copy link
Collaborator

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.

@DJWassink
Copy link

DJWassink commented Mar 21, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants