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

1st Saturday of Month error for Jan 2023 #6099

Open
codedynamics opened this issue Feb 8, 2023 · 3 comments
Open

1st Saturday of Month error for Jan 2023 #6099

codedynamics opened this issue Feb 8, 2023 · 3 comments

Comments

@codedynamics
Copy link

Describe the bug
Getting 1st Saturday of a Month error with Jan 2023 - returns 31st Dec 2022

Note: works fine for all other months in 2023, eg. for Feb it returns 04 Feb 2023

see example at: http://jsfiddle.net/oja704nq/

To Reproduce
let setMonth = 'Jan';
let setYear = 2023;
let sat_1_date = moment().set('year', setYear).set('month', setMonth).set('date', 1).isoWeekday(6);

console.log(sat_1_date) = Sat Dec 31 2022 13:29:35 GMT+1030 (Australian Central Daylight Time)

Expected behavior
I expected it to return 'Sat Jan 7 2023 13:29:35 GMT+1030 (Australian Central Daylight Time)'

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome
  • Version 109.0.5414.121

Moment-specific environment
moment-min.js version 2.29.4

** The time zone setting of the machine the code is running on **
Adelaide, Australia - Australian Central Daylight Time

** The time and date at which the code was run **
System date = 8/02/2023 - 1:29pm

** Please run the following code in your environment and include the output **
console.log((new Date()).toString()) = Wed Feb 08 2023 13:29:35 GMT+1030 (Australian Central Daylight Time)
console.log((new Date()).toLocaleString()) = 08/02/2023, 13:29:35
console.log((new Date()).getTimezoneOffset()) = -630
console.log(navigator.userAgent) = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
console.log(moment.version) = 2.29.4

Additional context
Note: I also tried using a numerical value for the month, eg. 0 instead of Jan

@IvaTsu
Copy link

IvaTsu commented Feb 9, 2023

Hey @codedynamics,

In short, if you will use moment().set('year', setYear).set('month', setMonth).set('date', 1).weekday(6) - it will return the first Saturday of the week: Sat Jan 07 2023.

It looks like the behavior of the .isoWeekday() and .weekday() differs in the way they find the nearest day of the week.

I believe that is related to the way the ISO system treats the day of the week. So, weekday() represents Sunday as 0 and Saturday as 6, while isoWeekday() represents Monday as 1 and Sunday as 7.

When using isoWeekday(6) -> January 1st, 2023 is a Sunday, and the nearest Saturday is December 31st, 2022 (as it is still the same week?). Hence, it is returning Saturday 31st December 2022 instead of the 7th of January, 2023.

When using weekday(6) -> the nearest Saturday is 7th January 2023, since January 1st, 2023 is a Sunday. The weekday() method does not adjust the date to the previous week, but the isoWeekday() method does as it was the same week.

@codedynamics
Copy link
Author

Hi @IvaTsu

Thanks for your response and assistance on this. Your reply has helped explain what was going on and also provided a solution!

PS. In your last paragraph you mention weekend(6) but I assumed you meant weekday(6) as weekend() is not a function of moment.js

@IvaTsu
Copy link

IvaTsu commented Feb 14, 2023

@codedynamics , edited the original answer. Indeed, I meant weekday(6) :)

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