Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calendar converter.

Status
Not open for further replies.

simonWMC2

Programmer
Aug 5, 2004
161
GB
Hi. I want a page that works out what the Julian date is. There are lots of examples on the web, but all are a bit complicated. A good one is -
However, all I want to do is work out the day, not the year. For example the 10 March would be day 069 (it is the 69th day of the year) I am assuming it is not a leap year.

I know i need to create a form where the user enters the date he /she wants converted and then use Javascript to do the conversion. I basically need the code to work out what month it is then add the days of all the previous month + the day of the current month. I would also like it to work the other way round, so the user can identify the real day from the Julian code.

I am getting quite confused, and if anyone could offer advice, I would be very happy

Thanx guys !
 
Found this doing a google search, didn't test it though.

Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- hide this script tag's contents from old browsers

function computeJD(formJD) {
    if (formJD.nmonth.value == "") {
      MM = 0
    } else {
      MM=eval(formJD.nmonth.value)
    }
    if (formJD.nday.value == "") {
      DD = 0
    } else {
      DD=eval(formJD.nday.value)
    }
    if (formJD.nyear.value == "") {
      YY = 0
    } else {
      YY=eval(formJD.nyear.value)
    }
    MM=eval(formJD.nmonth.value)
    DD=eval(formJD.nday.value)
    YY=eval(formJD.nyear.value)
    with (Math) {  
      GGG = 1;
      if (YY <= 1585) GGG = 0;
      JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
      S = 1;
      if ((MM - 9)<0) S=-1;
      A = abs(MM - 9);
      J1 = floor(YY + S * floor(A / 7));
      J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
      JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
      JD = JD + 1721027 + 2 * GGG + 367 * YY;
    }
    if (DD == 0 && MM == 0 && YY == 0) {
      alert("Please enter a meaningful date!")
    } else {
      formJD.JD.value = JD;
    }
}

function computeDJ(formDJ) {
  if (formDJ.JD.value == "") {
    alert("Please enter a meaningful Julian Day!")
    JD=0
  } else {
    JD=eval(formDJ.JD.value)
  }
  with (Math) {
    IJD = floor(JD);
    L = floor(IJD + 68569);
    N = floor(4 * L / 146097);
    L = L - floor((146097*N + 3)/4);
    I = floor(4000*(L + 1)/1461001);
    L = L - floor(1461 * I / 4) + 31;
    J = floor(80 * L / 2447);
    K = L - floor(2447 * J / 80);
    L = floor(J/11);
    J = J + 2 - 12*L;
    I = 100*(N - 49) + I + L;
  }
  if (JD != 0) {
    formDJ.nday.value = K;
    formDJ.nmonth.value = J;
    formDJ.nyear.value = I;
  }
}

function check_day(v) {
  if (v < 1 || v > 31 || v == "") {
    alert("Please enter a value for DAY in the 1-31 range or the result will be meaningless!")
    return ""
  } else {
    return true
  }
}

function check_month(v) {
  if (v < 1 || v > 12 || v == "") {
    alert("Please enter a value for MONTH in the 1-12 range or the result will be meaningless!")
    return ""
  } else {
    return true
  }
}
 
function check_year(v) {
  if (v <= 0 || v == "") {
    alert("Please enter a positive value for YEAR!")
    return ""
  } else {
    return true
  }
}

function check_JD(v) {
  if (v <= 0 || v == "") {
    alert("Please enter a positive value for Julian Day!")
    return ""
  } else {
    return true
  }
}

// done hiding from old browsers -->
</SCRIPT>

Code:
<FORM NAME=formJD>
Date (DD/MM/YYYY):
  <INPUT TYPE="text" NAME="nday"   Value="" SIZE=2
    ONCHANGE="check_day(this.value)"> /
  <INPUT TYPE="text" NAME="nmonth" Value="" SIZE=2
    ONCHANGE="check_month(this.value)"> /
  <INPUT TYPE="text" NAME="nyear"  Value="" SIZE=5>

<INPUT TYPE="button" VALUE="Corresponds to ->" OnClick="computeJD(this.form)">
<Br>

Julian Day : <INPUT TYPE="text" NAME="JD" Value="" SIZE=20>
</DL>

<INPUT TYPE="reset" Value="Reset Form">
</FORM>

<Hr align=center width=70%>

<DL>
<DT><B>JD to Date Conversion (JavaScript)</B>
<DD>
<FORM NAME=formDJ>
Julian Day : <INPUT TYPE="text" NAME="JD" Value="" SIZE=20
		ONCHANGE="check_JD(this.value)">

<INPUT TYPE="button" VALUE="Corresponds to ->" OnClick="computeDJ(this.form)">
<Br>

Date (DD/MM/YYYY):
  <INPUT TYPE="text" NAME="nday"   Value="" SIZE=2> /
  <INPUT TYPE="text" NAME="nmonth" Value="" SIZE=2> /
  <INPUT TYPE="text" NAME="nyear"  Value="" SIZE=5>

</DL>

<INPUT TYPE="reset" Value="Reset Form">
</FORM>

Dodge20
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top