CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Apr 1999
    Posts
    15

    How to check for month end n leap year

    Hi ppl,
    How can i check if its month end n if its a leap year??
    (if possible, do give me like a sample code etc)

    thanks



  2. #2
    Guest

    Re: How to check for month end n leap year

    I once saw some cool code that calculated the number of days in the month in a couple of lines of code. It relies on the bit pattern of the month numbers:

    int DaysInMonth(int nYear, int nMonth)
    {
    if(nMonth == 2) // February is a special case
    return (nYear % 4) == 0 ? 29 : 28; // Works for 2000, but not 1900 etc.
    else
    return 30 + ((( (nMonth & 0x8) >> 3) == (nMonth & 1)) ? 0 : 1);
    }

    void CDaysDlg::OnButton1()
    {
    int n = 0;
    n = DaysInMonth(1999, 1);
    n = DaysInMonth(1999, 2);
    n = DaysInMonth(1999, 3);
    n = DaysInMonth(1999, 8);
    n = DaysInMonth(1999, 9);
    n = DaysInMonth(1999, 12);
    n = DaysInMonth(2000, 2);
    n = DaysInMonth(1996, 2);
    n = DaysInMonth(1992, 2);
    n = DaysInMonth(99, 1);
    n = DaysInMonth(99, 2);
    n = DaysInMonth(99, 3);
    n = DaysInMonth(99, 12);
    n = DaysInMonth(00, 2);
    }

    Enjoy!
    Cheers,
    Paul


  3. #3
    Join Date
    Apr 1999
    Posts
    15

    Re: How to check for month end n leap year

    hi paul,
    thanks for the help...i have managed to check for month end but not a leap year...but will continue trying..
    If u happen to come across one that checks for leap year..do mail me again..
    thanks a lot :-)

    cheerz,
    Nelle


  4. #4
    Guest

    Re: How to check for month end n leap year

    // Example of the modulus operator
    int nCentury;

    if ((nCenturyYear % 4) == 0) {
    LeapYearFunction();
    }
    I find it in the MSDN help file, I think it can be useful.


  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: How to check for month end n leap year

    bool IsLeapYear( const int year )
    {
    return ( ( year % 4 != 0 ) || ( ( year % 100 == 0 ) && ( year % 400 != 0 ) ) ) ? false : true;
    }

    Sally


  6. #6
    Join Date
    Apr 1999
    Posts
    15

    Re: How to check for month end n leap year

    Thanks a lot..whoever u may be..
    i will try it out :-)

    cheerz
    Nelle


  7. #7
    Join Date
    Apr 1999
    Posts
    15

    Re: How to check for month end n leap year

    Thanks sally,
    i will try out what u gave me...:-)

    cheerz
    Nelle


  8. #8
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: How to check for month end n leap year

    Actually, the code that Sally gave is almost exactly correct, but all of her conditions must be met to satisfy the rules of a leap year, and the ternary operator in her sample code is redundant.

    Here is a nice quick little solution:

    BOOL IsLeapYear( const int nYear )
    {
    return ((nYear % 4) == 0) && ((nYear % 100) != 0) && ((nYear % 400) == 0);
    }

    On a leap year, we know that every 4 years is a leap year. We know that every 400 years is NOT a leap year, and we know that every 100 years will be a leap year EXCEPT for the 400th year. These rules are shown in the above code.

    Have fun!


    - Troy

  9. #9
    Join Date
    Apr 1999
    Posts
    15

    Re: How to check for month end n leap year

    thanks troy...
    u r the best!...not only did u give me the codes u even gave an explanation to it..at least i understand the codes now...thnks a lot :-)!!!

    cheerz
    Nelle


  10. #10
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: How to check for month end n leap year

    Troy wrote:

    BOOL IsLeapYear( const int nYear )
    {
    return ((nYear % 4) == 0) && ((nYear % 100) != 0) && ((nYear % 400) == 0);
    }

    On a leap year, we know that every 4 years is a leap year. We know that every 400 years is NOT a leap year, and we know that every 100 years will be a leap year EXCEPT for the 400th year. These rules are shown in the above code.

    Have fun!

    Actually the following is true:
    On a leap year, we know that every 4 years is a leap year. We know that every 100 years is NOT a leap year, unless it is every 400 years, so 1996 is a leap year, 2000 is a leap year, 1900 was NOT a leap year, 2100 was NOT a leap year

    I don't think Troy's code works that well, he has misunderstood leap years. If you use my code, it will work.

    Sally



  11. #11
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: How to check for month end n leap year

    Actually, thanks for pointing that out, that was a mistake in explanation on my part, but the code remains the same. It'll work for calculating leap years in the way explained by Sally. I fumbled my explanation, but my code is still cleaner and less redundant.

    Sally:
    Look at your code in the previous example, it *does not* meet the conditions that you just stated, then look at mine, and you'll see that my code does satisfy those conditions.

    - Troy

  12. #12
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: How to check for month end n leap year

    Let's apply some years to your formula:
    ((nYear % 4) == 0) && ((nYear % 100) != 0) && ((nYear % 400) == 0);

    1995: ((1995 % 4) == 0) is FALSE,so it is not a leap year
    1996: ((1996 % 4) == 0) is TRUE, so it can be a leap year, let's continue:
    ((1996 % 100) != 0) is TRUE, so still it could be a leap year, then:
    ((1996 % 400) == 0) is FALSE, so since TRUE && TRUE && FALSE makes FALSE
    1996 is NOT a leap year according to your piece of code......

    I'd recommend the following change to your function:
    ((nYear % 4) == 0) || ((nYear % 100) != 0) && ((nYear % 400) == 0);

    Sally


  13. #13
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: How to check for month end n leap year


    Actually, this is what it should look like.....

    ((nYear % 4) == 0) || (((nYear % 100) != 0) && ((nYear % 400) == 0));

    Sally



Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured