From: Branko Radovanovic
Subject: Day difference between two dates
Message-ID: <199602161231.NAA18023@pinus.cc.etf.hr>
Date: Fri, 16 Feb 1996 13:31:39 +0100

I suggest the following code for solving both the problem of computing the day difference between two dates, and the problem of determining day of week on a given date :

#define DayOfWeek(d,m,y) (int)(DaysElapsed(d,m,y) % 7)

long DaysElapsed(int d,int m,int y) {
    static int cd[]={0,31,59,90,120,151,181,212,243,273,304,334};
    long n=365L*(y-1);

    if (m<3) y--;
    return n+y/4-y/100+y/400+cd[m-1]+d;
}

DayOfWeek macro works exactly the same as the code posted by Tomohiko Sakamoto, while the day difference between date1 and date2 (date1<=date2) can simply be computed this way:

difference=DaysElapsed(d2,m2,y2)-DaysElapsed(d1,m1,y1);

I think this is by far superior to using mktime(). The code is 100 % portable (even across languages other than C), much faster, smaller and simpler than mktime(), and does not suffer from year range limitation.

Branko Radovanovic