Mod command isn't working... I think

Hi All.

Here is a really stupid question. I am trying to determine if February has 29 days in it, I use this:

//MessageBox "It is February"
MessageBox "The year is " + newMortgageAmortizationProgramWindow.DateTimePicker1.SelectedDate.Year.ToString
if newMortgageAmortizationProgramWindow.DateTimePicker1.SelectedDate.Year mod 2 = 0 then
  numberOfDaysInAMonth = 28
else
  MessageBox "Leap Year"
  numberOfDaysInAMonth = 29
end if

And no matter whether I use the DatePicker1.SelectedDate.Year or an actual number, I always go to a leap year. If I do a mod 2 or mod 4 I still get the same thing.

What is wrong with this code?

Regards

Mod 2 just checks if the number is odd or even.

You could use variables.
Put the year in one and the mod result in an integer and check in debugger.

Use <> instead of = after mod? or put the 29 days when true and 28 days when false?

If you test 2023 mod 2 = 0 then this will be false and will push you to the code for leap year. The same for mod 4 = 0 (2023 will be false and then use the else section).

To determine whether a year is a leap year, follow these steps:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days).
  5. The year is not a leap year (it has 365 days).

This is not an operator precedence issue, is it?

Don’t forget the other Leap Year rules.

1 Like

most examples i saw create a date with the first day at next month and subtract a day.
via DateInterval or SubtractInterval
means you get the last day of month 28 or 29 in your case.

1 Like

https://forum.xojo.com/t/datetime-isleapyear-extension-method/68233

1 Like

Why not simply try and create a date with the 29th Feb for the year. If it is invalid it should result in either an invalid date, an exception or a date which isn’t the 29th. Let the system / xojo do the work.

1 Like