Philadelphia Reflections

The musings of a physician who has served the community for over six decades

Related Topics

George and Computers(1)
I got him into computers around 1960. He soon far surpassed me.

Date Math

Figuring out how many days there are between two dates is either built in to the system you are using or else you will tear your hair out what with leap years, floating leap days, etc.

There are two ways to do this fairly simply:

  1. Using Julian Date Counts
  2. Using the system clock functions

Julian Date Counts

Julian Date Counts are not related to Julius Caesar's calendar. They are a way to assign a sequential number to every date going way back into antiquity. Convert any two dates to their Julian Date Count number and the difference is the number of days between the dates.

See Julian Day Numbers

In C++ ...

long int GregorianToJulian(int gregorianMonth, int gregorianDay, int gregorianYear)
	// this function calculates the Julian Date Number from a Gregorian date
	// astronomers and others use these numbers becasue the difference between two
	// Julian Date Numbers is the number of days between the dates
	// see

	long int A;
	long int B;
	long int C;
	long int E;
	long int F;
	long int JD;

	long int Y, M, D;

	Y = gregorianYear;
	M = gregorianMonth;
	D = gregorianDay;

	if (M <= 2)
	  Y = Y - 1;
	  M = M + 12;
	A = Y/100;
	B = A/4;
	C = 2-A+B;
	E = 365.25*(Y+4716);
	F = 30.6001*(M+1);
	JD= C+D+E+F-1524.5;

	return JD;

void JulianToGregorian (long int julianDateNumber, int& gregorianMonth, int& gregorianDay, int& gregorianYear)
	// Convert a Julian Date Number to a Gregorian Date
	// see

	long int Z;
	long int W;
	long int X;
	long int A;
	long int B;
	long int C;
	long int D;
	long int E;
	long int F;

	Z = julianDateNumber+0.5;
	W = (Z - 1867216.25)/36524.25;
	X = W/4;
	A = Z+1+W-X;
	B = A+1524;
	C = (B-122.1)/365.25;
	D = 365.25*C;
	E = (B-D)/30.6001;
	F = 30.6001*E;

	gregorianDay   = B-D-F;
	gregorianMonth = ((E-1) > 12) ? E-13 : E-1;
	gregorianYear  = (gregorianMonth>2) ? C-4716 : C-4715;

Using the system clock functions

Again in C++ ...


Find the days between two dates

Found at

The sources were helpful but I had to extensively modify/hack to make this actually work


#include <time.h>
#define SECONDS_PER_DAY (24 * 60 * 60)

time_t time_from_date(int year, unsigned int month, unsigned int day)
    // Return the time from the year 1900 to the date entered
    struct tm a = {0,0,0,day,month - 1,year - 1900};
    time_t x = mktime(&a);

    return x;

int days_between(int year0, unsigned month0, unsigned day0,
                 int year1, unsigned month1, unsigned day1)
	// The difference in seconds between two dates,
	//   divided by the number of seconds in a day ...
	//     is the number of days between the dates

    return difftime(time_from_date(year1, month1, day1),
                    time_from_date(year0, month0, day0)) / SECONDS_PER_DAY;

(my thanks to for HTML encoding)


Please Let Us Know What You Think


(HTML tags provide better formatting)