Ok, I tried and tried some more, if someone could push me in a right direction or point out the obvious for me that would be great.

The dates have to be between the years 1900 and 2099, so I was thinking if I could calculate the amount of days from 1900 to each date..I could then compare the difference. But that darn leap year just messes everything up. Below is some code I was working on..but sadly I don't think it will work.

Do I need to do one long loop that looks at every year to see if its normal or leap? Then add 365 or 366 to each running total. I suppose I could do that, but that doesn't seem like the best programming.

const int daysInYear    [] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const int daysInLeapYear[] = {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

const int daysInMonth         [] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int DaysInMonthLeap     [] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int dayCaculator (int year, int month, int day)
{
    int days;

    if ( year %4 == 0 && year !=0 ) //leap year
    {
        days = year*365 + (year-1)/4 + daysInLeapYear[month] + day ;
    }
    else  // not leap year
    {
            days = year*365 + (year-1)/4 + daysInYear[month] + day ;
    }


    return days;

Recommended Answers

All 2 Replies

can you use the functions in time.h? If you can, just populate a struct tm, call mktime() to get the time_t object. Once you have the two time_t objects call difftime().

Leap year is a little bit more complex. If year % 4 == 0 , but, if year % 100 == 0 it's not, unless year % 400 == 0. That is, only every fourth century year (every 400 years) is a leap year.

Perhaps an approach would be to just assume 365 day years, and determine how many leap years occurred between your base date and the date in question, add that many days to the count.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.