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
April 29th, 1999, 02:54 AM
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
Nelle
April 29th, 1999, 08:39 PM
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
April 29th, 1999, 09:33 PM
// 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.
sally
April 29th, 1999, 10:10 PM
bool IsLeapYear( const int year )
{
return ( ( year % 4 != 0 ) || ( ( year % 100 == 0 ) && ( year % 400 != 0 ) ) ) ? false : true;
}
Sally
Sally
April 29th, 1999, 10:10 PM
bool IsLeapYear( const int year )
{
return ( ( year % 4 != 0 ) || ( ( year % 100 == 0 ) && ( year % 400 != 0 ) ) ) ? false : true;
}
Sally
Nelle
April 29th, 1999, 10:24 PM
Thanks a lot..whoever u may be..
i will try it out :-)
cheerz
Nelle
Nelle
April 29th, 1999, 10:25 PM
Thanks sally,
i will try out what u gave me...:-)
cheerz
Nelle
Troy T
April 30th, 1999, 01:26 AM
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.
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
Nelle
May 2nd, 1999, 08:37 PM
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 :-)!!!
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.
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
Troy T
May 3rd, 1999, 11:06 PM
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
sally
May 9th, 1999, 06:21 AM
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
Sally
May 9th, 1999, 06:21 AM
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);