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

weekday(-dayOfWeek) goes back two weeks instead of 1 #1336

Closed
ajenkinski opened this issue Jan 14, 2021 · 2 comments
Closed

weekday(-dayOfWeek) goes back two weeks instead of 1 #1336

ajenkinski opened this issue Jan 14, 2021 · 2 comments

Comments

@ajenkinski
Copy link

Describe the bug
When I use the weekday plugin to find the previous instance of a day of the week, for example the last Sunday, it goes back two weeks instead of one. For example the documentation for the weekday plugin shows this example:

dayjs().weekday(-7); // last Sunday

which seems to indicate this would return the most recent Sunday. However the behavior I actually get is it returns the date of the Sunday before last.

Expected behavior

Here is a reproduction of the problem in the Node.js REPL.

> dayjs = require('dayjs')
> dayjs.extend(require('dayjs/plugin/weekday'))

> dayjs().format('YYYY-MM-DD')
'2021-01-14'
> dayjs().weekday(7).format('YYYY-MM-DD')
'2021-01-17'
> dayjs().weekday(-7).format('YYYY-MM-DD')
'2021-01-03'

As you can see, the current date when I ran this was 2021-01-14. I expected weekday(-7) to return the previous Sunday, 2021-01-10, but instead it returned 2021-01-03, which is the Sunday before the previous Sunday.

Information

  • Day.js Version: v1.9.3
  • OS: MacOS 10.15.7
  • Browser: Node.js 12.5.0
  • Time zone: GMT-05:00 EST
@ajenkinski ajenkinski changed the title weekday(-dayOfWeek) goes back two weeks instead of 1 weekday(-dayOfWeek) goes back two weeks instead of 1 Jan 14, 2021
@iamkun
Copy link
Owner

iamkun commented Jan 15, 2021

This is the expected result.
As mentioned in doc

If the locale assigns Sunday as the first day of the week, dayjs().weekday(0) will be Sunday.

so, dayjs().weekday(-7) will be 7 days before Sunday

@ajenkinski
Copy link
Author

Thanks. It makes sense to me now.

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

2 participants