<!--

//http://www.hermetic.ch/cal_stud/jdn.htm
//http://home.capecod.net/~pbaum/date/injdimp.htm

// requires dow(jdn), DAYS, MONTHS, DateStruct(), trunc()

/////////////////////////////////////////////////
// http://home.capecod.net/~pbaum/date/date0.htm
// Copyright Peter Baum
//
// valid for all dates
/////////////////////////////////////////////////
function GregorianToJDN(y,m,d)
{
  var jd = 0;
 
  if (m < 3)
  {
    m += 12; 
    y -= 1; 
  }  
  jd = d + (((153 * m) - 457) / 5) + (365 * y) + trunc(y / 4) - trunc(y / 100) + trunc(y / 400) + 1721118.5; 
  jd += 0.5; // calculate Chronological Julian Day

  return jd;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// For dates in the Gregorian calendar at noon, The Explanatory Supplement to the Astronomical Almanac,1992, 
// page 604, gives an algorithm by Henry F. Fliegel and Thomas C. Van Flandern that calculates dates after 
// November 23, -4713, given an integral Julian Day Number. This algorithm can be shown to be an instance of 
// the general form described in the algorithm development section. 
//
// valid (only) for dates from -4800-03-01 G onward when converting to a Julian day number from a date
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GregorianToJDN2(y,m,d)
{
  if ((y > -4800) || (((y == -4800) && (m > 3)) || ((y == -4800) && (m == 3) && (d > 1))))
  {
    return ( trunc((1461 * ( y + 4800 + trunc((m - 14) / 12))) / 4) +
             trunc((367 * (m - 2 - (12 * trunc((m - 14) / 12)))) / 12) -
             trunc((3 * trunc((y + 4900 + trunc((m - 14) / 12)) / 100)) / 4) +
             d - 32075 );          
  }          
  else
  {
    return -32044;
  }
}

/////////////////////////////////////////////////
// http://home.capecod.net/~pbaum/date/date0.htm
// Copyright Peter Baum
//
// valid for all dates
/////////////////////////////////////////////////
function JDNToGregorian(jd) 
{
  var date = new DateStruct();

  var z, r, g, a, b, c, d, m, y;
  
  jd -= 0.5; // calculate Chronological Julian Day
  z = Math.floor(jd - 1721118.5); 
  r = jd - 1721118.5 - z; 
  g = z - .25; 
  a = Math.floor(g / 36524.25); 
  b = a - Math.floor(a / 4); 
  y = Math.floor((b+g) / 365.25); 
  c = b + z - Math.floor(365.25 * y); 
  m = trunc(((5 * c) + 456) / 153); 
  d = c - trunc(((153 * m) - 457) / 5) + r; 
  if (m > 12)
  {
    y += 1; 
    m -= 12; 
  }

  date.year  = y;
  date.month = m;
  date.day   = d;
  
  date.monthStr = MONTHS[date.month-1];
  
  date.dayStr   = DAYS[dow(jd)];	

  return date;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// For dates in the Gregorian calendar at noon, The Explanatory Supplement to the Astronomical Almanac,1992, 
// page 604, gives an algorithm by Henry F. Fliegel and Thomas C. Van Flandern that calculates dates after 
// November 23, -4713, given an integral Julian Day Number. This algorithm can be shown to be an instance of 
// the general form described in the algorithm development section. 
//
// valid (only) for dates from -4900-03-01 G onward when converting from a Julian day number to a date
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function JDNToGregorian2(jd) 
{
  var date = new DateStruct();

  var l, n, i, j, d, m, y;
  if (jd >= -68569)
  {
    l = jd + 68569;
    n = trunc((4 * l) / 146097);
    l = l - trunc(((146097 * n) + 3) / 4);
    i = trunc((4000 * (l + 1)) / 1461001);
    l = l - trunc((1461 * i) / 4) + 31;
    j = trunc((80 * l) / 2447);
    d = l - trunc((2447 * j) / 80);
    l = trunc(j / 11);
    m = j + 2 - (12 * l);
    y = (100 * (n - 49)) + i + l;
        
    date.year  = y;
    date.month = m;
    date.day   = d;
   
    date.monthStr = MONTHS[date.month-1];

    date.dayStr   = DAYS[dow(jd)];		
  }
  else
  {
    date.year  = 0;
    date.month = 0;
    date.day   = 0;
  }

  return date;
}

-->

