获取某月具体的天数:
int GetMonthDay(int year, int month)
{
int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month==2 && IsLeapYear(year))
{
return days[month] + 1;
}
return days[month];
}
判断是否为闰年:
bool IsLeapYear(int year)
{
if (
((year%4==0)&&(year%100!=0))
|| (year%400==0)
)
{
return true;
}
return false;
}
完整代码:
Date operator+(int day)
{
Date temp(*this);
//天数相加
temp._day += day;
//判断是否超天数
while (temp._day > GetMonthDay(temp._year, temp._month))
{
temp._day -= GetMonthDay(temp._year, temp._month);
temp._month++;
if (temp._month == 13)
{
temp._year++;
temp._month = 1;
}
}
return temp;
}