#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool ifleapyear(int n)
{
if (((n % 4 == 0) && (n % 100 != 0)) || (n % 400 == 0))
{
return true;
}
return false;
}
int countYearDays(char *begin, char *end)
{
int count = 0, i = 0, j = 0;
int leapyear[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int comyear[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int beginyear = 0, beginmon = 0, beginday = 0;
int endyear = 0, endmon = 0, endday = 0;
sscanf(begin, "%d - %d - %d", &beginyear, &beginmon, &beginday);
sscanf(end, "%d - %d - %d", &endyear, &endmon, &endday);
for (i = beginyear; i < endyear + 1; i++)
{
if (ifleapyear(i))
{
if (beginyear == endyear)
{
if (beginmon == endmon)
{
count += (endday - beginday);
}
else
{
count += (leapyear[beginmon - 1] - beginday);
count += endday;
for (j = beginmon; j < endmon - 1; j++)
{
count += leapyear[j];
}
}
}
else
{
if (i == beginyear)
{
count += (leapyear[beginmon - 1] - beginday);
for (j = beginmon; j < 12; j++)
{
count += leapyear[j];
}
}
else if (i == endyear)
{
count += endday;
for (j = 0; j < endmon - 1; j++)
{
count += leapyear[j];
}
}
else
{
for (j = 0; j < 12; j++)
{
count += leapyear[j];
}
}
}
}
else
{
if (beginyear == endyear)
{
if (beginmon == endmon)
{
count += (endday - beginday);
}
else
{
count += (comyear[beginmon - 1] - beginday);
count += endday;
for (j = beginmon; j < endmon - 1; j++)
{
count += comyear[j];
}
}
}
else
{
if (i == beginyear)
{
count += (comyear[beginmon - 1] - beginday);
for (j = beginmon; j < 12; j++)
{
count += comyear[j];
}
}
else if (i == endyear)
{
count += endday;
for (j = 0; j < endmon - 1; j++)
{
count += comyear[j];
}
}
else
{
for (j = 0; j < 12; j++)
{
count += comyear[j];
}
}
}
}
}
return count;
}
int main()
{
printf("2011-11-18 2022-11-18 count=%d\n", countYearDays("2011-11-18", "2022-11-18"));
printf("2018-11-18 2022-11-18 count=%d\n", countYearDays("2018-11-18", "2022-11-18"));
printf("2015-11-18 2022-11-18 count=%d\n", countYearDays("2015-11-18", "2022-11-18"));
printf("2000-11-18 2022-11-18 count=%d\n", countYearDays("2000-11-18", "2022-11-18"));
printf("2022-10-1 2022-11-18 count=%d\n", countYearDays("2022-10-1", "2022-11-18"));
printf("2022-11-1 2022-11-18 count=%d\n", countYearDays("2022-11-1", "2022-11-18"));
system("pause");
return 0;
}