Question:
How can I create a mathematical logic for leap years?
Solution:
Leap years are years multiples of 4 but not of 100, unless they are multiples of 400. This happens because a year is approximately 365.2425 days long.
To find out whether a certain year is a leap year, use the following logic:
Sub CommandButton1_Click()
'Logic for checking whether current year (using Now* method) is a leap year
If (Year(Now()) Mod 4 = 0 And Year(Now()) Mod 100 <> 0 Or Year(Now()) Mod 400 = 0) = True Then
MsgBox ""&Year(Now())&" is a leap year!"
Else
MsgBox ""&Year(Now())&" is not a leap year!"
End If
End Sub
NOTE: Now() method returns the current date and time of the machine where it runs.
Thus, you will notice that 2014, for example, is not a leap year:
2014 / 4 = 503,5 (False)
2014 / 100 = 20,14 (True)
2014 / 400 = 5,035 (False)
Logic = False
However, 2016 is a leap year:
2016 / 4 = 504 (True)
2016 / 100 = 20,14 (True)
2016 / 400 = 5,035 (False)
Logic = True
This logic can be applied to annual procedures, such as monthly reports, digital time programming, holidays, etc.