VOID GetGregorianDate(LONG JD, PWORD Year, PWORD Month, PWORD Day)

{

LONG j, y, d, m;

//while calculating we consider the following :

//for Years -> 4 centuries (146067 days)

//for Months -> 4 years and 5 consecutive months

//for days -> 5 consecutive months

j = JD - 1721119; //1721119 is the number of days since JD 0 to the start of March 2 1BC. //hence j are the no of days from March 2 1BC

y = (4 * j - 1) / 146097; //146097 is the number of days in four centuries, time it takes for the cycle of leap years to repeat.

j = 4 * j - 1 - 146097 * y; //4 is used to denote leap year

d = j / 4;

j = (4 * d + 3) / 1461; //number of days in four years.

d = 4 * d + 3 - 1461 * j;

d = (d + 4) / 4;

m = (5 * d - 3) / 153; //number of days in 5 consecutive months alternating between 31 and 30 days (either Mar-Jul or Aug-Dec).

d = 5 * d - 3 - 153 * m;

d = (d + 5) / 5;

y = 100 * y + j;

if (m < 10)

{

m = m + 3; //additions and subtractions of 3 and 9 are to restore the "start" of the year to January.

}

else

{

m = m - 9; //additions and subtractions of 3 and 9 are to restore the "start" of the year to January.

y = y + 1;

}

*Year = (WORD) y;

*Month = (WORD) m;

*Day = (WORD) d;

}

Tweet This+ 1 thisPost To Linkedin