Skip to content

Instantly share code, notes, and snippets.

@cpylua
Created April 22, 2012 14:19
Show Gist options
  • Save cpylua/2464302 to your computer and use it in GitHub Desktop.
Save cpylua/2464302 to your computer and use it in GitHub Desktop.
computing the day difference between two dates
/* this code is from http://c-faq.com/lib/calendar.br.html */
#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;
}
difference=DaysElapsed(d2,m2,y2)-DaysElapsed(d1,m1,y1);
/**
* Here are some long explanations:
* http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
* http://alcor.concordia.ca/~gpkatch/gdate-method.html
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment