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

Reliable method for next / previous weekday #1517

Open
thomassth opened this issue Oct 3, 2023 · 4 comments · May be fixed by #1538
Open

Reliable method for next / previous weekday #1517

thomassth opened this issue Oct 3, 2023 · 4 comments · May be fixed by #1538

Comments

@thomassth
Copy link
Contributor

thomassth commented Oct 3, 2023

Describe the bug
DateTime.set({ weekday: X }) sometimes returns a date in the past, and sometimes in the future.
To Reproduce

luxon.DateTime.fromISO('2023-10-01T20:24:00Z').set({ weekday: 3 }).toISO()
>'2023-09-27T16:24:00.000-04:00'
luxon.DateTime.fromISO('2023-10-02T20:24:00Z').set({ weekday: 3 }).toISO()
>'2023-10-04T16:24:00.000-04:00'

Actual vs Expected behavior
All values return a future date

Additional context
I think in moment.js, setting a weekday will always return a future date

@thomassth thomassth changed the title Inconsistent set weekday behavior Reliable method for next / previous weekday Oct 4, 2023
@mrlubos
Copy link

mrlubos commented Oct 4, 2023

This sounds like an expected behaviour. You're setting day of the current week, so it can be both in the future and in the past.

@diesieben07
Copy link
Collaborator

A reliable way to do this would be:

function nextWeekday(date, weekday) {
  return date.plus({days: (7 - date.weekday + weekday) % 7});
}

I'll mark this as a feature request to potentially introduce DateTime#nextWeekday and DateTime#previousWeekday methods.

@shashankbhat10
Copy link

Hey @diesieben07 , can I raise a PR for this. Have added the suggested methods and respective tests.

@diesieben07
Copy link
Collaborator

Of course, go ahead @shashankbhat10

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