[HOWTO] ⏰ Five Ways To Check If A Year Is A Leap Year

As per the [Guide] How to use the CLOCK guide, here are 2 simple ways to check if a year is a leap year or a common year.

The question is also available here.

Definition of a leap year

Before we begin, you need to know the following.

  • 2024 is a leap year because 2024 is divisible by 4 and not by 100.

    • So technically, the remainder of 2024 ÷ 4 = 0, but the remainder of 2024 ÷ 100 ≠ 0.

    • If a year is divisible by 4 and not by 100, it is a leap year.

  • 2022 is not a leap year because 2022 is not divisible by 4.

    • So technically, the remainder of 2022 ÷ 4 ≠ 0.

    • If a number is not divisible by 4, it is not a leap year.

  • 1900 is NOT a leap year because although it is divisible by 4, for years that are also divisible by 100, they must be divisible by 400 at the same time.

    • So technically, the remainder of 1900 ÷ 4 = 0, and that the remainder of 1900 ÷ 100 = 0, but the remainder of 1900 ÷ 400 ≠ 0.

    • If a number is divisible by 100, it must be also divisible by 400 to be a leap year.

  • 2000 is a leap year because 2000 is divisible by 400.

    • If a number is divisible by 100, it must be also divisible by 400 to be a leap year.

#1 - With the Clock component

In this concept, we determine the day before March 1 of that year.

  • the day before March 1 in 2022 is February 28, 2022.

  • we extract the day of month of February 28, and then find that the day of month is 28.

  • if the last day of a year in February is 29, it is a leap year. Otherwise, it is not.

#2 - Without the Clock component

With the definition of what is a leap year, and determining the year, we can also check whether a year is a leap year just by looking at itself.

  • if a year is divisible by 400, it passes.

  • in the next if-else, if a year is divisible by 100, it does not pass. This is not a bug. If it does not pass the first if-else statement, it means that it is not divisible by 400. Now, this statement proves that it is divisible by 100, so that means it is a common year.

  • In the third if-else statement, if the year is not divisible by 100 or 400, but is divisible by 4, it passes.

  • Else if the year gets here, it does not pass.

#3 - With the Clock component and milliseconds

Prior to method #1, this is also a way to get the day before Marcg but with milliseconds.

#4 + #5 - Get day of year for December 31

With Ram's idea, we get the number of day for the last day of year via MakeInstant.

Here is my other way to go.

:heavy_check_mark: Tests

Tested successfully on the AI Companion on Xiaomi 5G 11 NE Lite.

:+1: Rate my tutorial! :-1:

  • Good tutorial!
  • Bad tutorial.
0 voters

Kindly :email: PM me if you have any questions! Also, if you like my tutorial, please :heart: like it! It takes some effort for me to make it...

Votes and likes tell me the general user feedback of my tutorial. If you read this tutorial, please take 20 seconds to drop by and give a vote / like!

If you have any features that you want to add to this tutorial, PM me.


Gordon Lu

:speech_balloon: Message :earth_africa: Website :e-mail: E-mail

9 Likes

Another way to get the day before March is via calculating milliseconds.

1 day = 1 ✕ 1,000 ✕ 60 ✕ 60 ✕ 24 = 86,400,000 (milliseconds)

So use this concept to have method number #3.

1 Like

Thank you very much for tutorial @gordonlu310

1 Like

Or this way

image

5 Likes

Seems like we already have 4 methods. Here is a fifth one I just found.

This is a snippet from @Taifun's website about how to get day of year.

If the last day in the year is the 366th day in the year, it is a leap year. Else it is a common year.

1 Like

@Kevinkun All working fine as intended.

image

image

1 Like

hi i need help but it is n another post

sorry coz that's kind of spam and it's kind of irritating, so yeah...