Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Currently in my code i will get some date from the file reading like 'CString tsDate' in the format of YYYYMMDD(ex: 20180501)
and i need to check whether 'tsDate' is last day of that month. It means for MAY month it is 20180531 so i nned to get this date.

In my code currently am using COleDateTime class to get GetMonth() and GetYear.

Can anyone suggests me to get the last date of the month?

What I have tried:

I have tried the below :
int getNumberOfDays(int month, int year)
{
//leap year condition, if month is 2
if( month == 2)
{
if((year%400==0) || (year%4==0 && year%100!=0))
return 29;
else
return 28;
}
//months which has 31 days
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
||month == 10 || month==12)
return 31;
else
return 30;
}

But here months are hardcoded but every year number of days may vary right?
Posted
Updated 12-Nov-18 12:30pm

Quote:
But here months are hardcoded but every year number of days may vary right?
Just February days may vary, anyway the posted code correctly handles it.

As an alternative, you could also add a day to your COleDateTime object (using a 1-day COleDateTimeSpan object) and see if there is a month change.
 
Share this answer
 
Quote:
But here months are hardcoded but every year number of days may vary right?

How is it possible to not know how Gregorian calendar is working and have correct code ?
The question let us wonder if you just know how it works and that you didn't find on internet :
Gregorian calendar - Wikipedia[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900