What follows is the JavaScript Code for the Due Date Calculator. The logic for drawing the calendar is from Dave Eisenberg's calendar. If you have any questions or comments about the code, contact me at .


<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
<!-- HIDE THE SCRIPT FROM OTHER BROWSERS

function monthArray(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12) {
  this[0] = m1;
  this[1] = m2;
  this[2] = m3;
  this[3] = m4;
  this[4] = m5;
  this[5] = m6;
  this[6] = m7;
  this[7] = m8;
  this[8] = m9;
  this[9] = m10;
  this[10] = m11;
  this[11] = m12;
}

function calcDueDate(menstDay) {
  var dueDate =Date.parse(menstDay.toLocaleString());
  // 1 day=1 ms * 1000= 1 sec * 60 = 1min * 60 = 1 hr * 24
  var gestation = 280 * 1000* 60 * 60 * 24;
  dueDate = dueDate + gestation;
  return new Date(dueDate);
}

function getMonthDays(day) {
  var monthDays = new monthArray(31, 28, 31, 30, 31, 30, 31, 31, 30,
      31, 30, 31);
  // do the classic leap year calculation
  if (((day.getYear() % 4 == 0) && (day.getYear() % 100 != 0)) || (day.getYear() % 400 == 0))
    monthDays[1] = 29;
  var numDays=monthDays[day.getMonth()];
  return numDays;
}

function setDayOpts(day) {
  var numDays = getMonthDays(day);
  var selNum = document.dateSetter.selDay.selectedIndex;
  for (i=30; i>=0; i--) {
    if (i>=numDays) {
      document.dateSetter.selDay.options[i] = null;
    }
    else {
      document.dateSetter.selDay.options[i] =
      new Option(i+1, i, false, false) ;
    }
  }
  if (selNum < numDays) {
    document.dateSetter.selDay.selectedIndex = selNum;
  }
  else {
    document.dateSetter.selDay.selectedIndex = numDays-1;
  }
}

function drawCal(day) {
  var thisDay;
  thisDay = day.getDate();
  var year = day.getYear();

  //deal with two digit dates
  if (year < 100) {
    year = year + 1900;
  }
  //now deal with that funky y2k problem
  if (year < 1970) {
    year = year + 100;
  }

  var nDays = getMonthDays(day);
  // and go back to the first day of the month...
  var firstDay = day;
  firstDay.setDate(1);
  // and figure out which day of the week it hits...
  var startDay = firstDay.getDay();

  parent.Due.document.close();
  parent.Due.document.open();
  parent.Due.document.writeln("<CENTER>");
  parent.Due.document.writeln("<H2>YOUR DUE DATE IS</H2>");
  parent.Due.document.write("<TABLE BORDER>");
  parent.Due.document.write("<TR><TH COLSPAN=7>");
  parent.Due.document.write(document.dateSetter.selMonth.options[day.getMonth()].text);
  parent.Due.document.write(" ");
  parent.Due.document.write(year);
  parent.Due.document.write("<TR><TH>Sun<TH>Mon<TH>Tue<TH>Wed<TH>Thu<TH>Fri<TH>Sat");
  // write the blanks at the beginning of the calendar
  parent.Due.document.write("<TR>");
  column = 0;
  for (i=0; i<startDay; i++) {
    parent.Due.document.write("<TD>");
    column++;
  }
  for (i=1; i<=nDays; i++) {
    parent.Due.document.write("<TD>");
    if (i == thisDay)
      parent.Due.document.write("<FONT COLOR=\"#FF0000\">")
    parent.Due.document.write(i);
    if (i == thisDay)
      parent.Due.document.write("</FONT>")
    column++;
    if (column == 7) {
      parent.Due.document.write("<TR>"); // start a new row
      column = 0;
    }
  } // for (i=1; i<=nDays; i++)
  parent.Due.document.write("</TABLE>");
  parent.Due.document.writeln("</CENTER>");
  parent.Due.document.writeln("");
  parent.Due.document.close();

}

// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>

</HEAD>
<BODY>
<CENTER>
Select the first day of your last period:
<BR>
<FORM NAME ="dateSetter">
<SELECT NAME="selMonth" onChange="setDayOpts(new Date(
            document.dateSetter.selYear.selectedIndex+1996,
            document.dateSetter.selMonth.selectedIndex, 1));">
<OPTION>January</OPTION>
<OPTION>February</OPTION>
<OPTION>March</OPTION>
<OPTION>April</OPTION>
<OPTION>May</OPTION>
<OPTION>June</OPTION>
<OPTION>July</OPTION>
<OPTION>August</OPTION>
<OPTION>September</OPTION>
<OPTION>October</OPTION>
<OPTION>November</OPTION>
<OPTION>December</OPTION>
</SELECT>

<SELECT NAME="selDay">
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
<OPTION></OPTION>
</SELECT>

<SELECT NAME="selYear" LANG = JS onChange="setDayOpts(new Date(
            document.dateSetter.selYear.selectedIndex+1996,
            document.dateSetter.selMonth.selectedIndex,1));">
<OPTION>1996</OPTION>
<OPTION>1997</OPTION>
<OPTION>1998</OPTION>
<OPTION>1999</OPTION>
<OPTION>2000</OPTION>
</SELECT>

<INPUT TYPE="button" VALUE="Calculate Due Date" onClick="drawCal(
  calcDueDate(
  new Date(document.dateSetter.selYear.selectedIndex+1996,
      document.dateSetter.selMonth.selectedIndex,
      document.dateSetter.selDay.selectedIndex+1)
  ));">

</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE THE SCRIPT FROM OTHER BROWSERS
  var today = new Date();

  var browser =navigator.appName;
  setDayOpts(today);

  document.dateSetter.selMonth.options[today.getMonth()].selected = true;
  document.dateSetter.selDay.options[today.getDate()-1].selected = true;
  var Year = today.getYear(); //pitfall-don't forget the () or you get weird results
  if (Year < 100)
    Year += 1900;
  //now deal with that funky y2k problem
  if (Year < 1970) {
    Year = Year + 100;
  }

  document.dateSetter.selYear.options[Year-1996].selected = true;



// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
<CENTER>
</BODY>
</HTML>
return to geekstuff