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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Julian Date Converter 2

Status
Not open for further replies.

simonWMC2

Programmer
Aug 5, 2004
161
GB
Thanks to Kaht who gave me this code to convert a calendar date to a julian date.

Code:
<script type="text/javascript">

//set an array with day counts for all months
var dayCount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function convertDate(str) {
   //convert passed string to date object
   var dte = new Date(str);
   //initialize date variable
   var julianDate = 0;
   //add days for previous months
   for (i = 0; i < dte.getMonth(); i++) {
      julianDate += dayCount[i];
   }
   //add days of the current month
   julianDate += dte.getDate();
   //check for leap year
   if (dte.getFullYear() % 4 == 0 && dte.getMonth() > 1) {
      julianDate++;
   }
   alert("Julian Date: " + julianDate);
   return julianDate;
}

</script>

<body>
<input type="text" id="txt" /><br>
<input type="button" value="get julian date" onclick="convertDate(document.getElementById('txt').value)" />
</body>

Can anyone tell me how i can change the input format from mm/dd/yy to dd/mm/yy.

I would also like to show the result in a text box rather than an alert.

Sorry to be a pain but I am still not very good at javascript and am feeling confused.
 
Drop this into the start if the function:
Code:
function convertDate(str) {
[!]  var str_mm = str.split("/")[0];
  var str_dd = str.split("/")[1];
  var str_yy = str.split("/")[2];
  str = str_dd + "/" + str_mm + "/" + str_yy;[/!]
  //convert passed string to date object
  var dte = new Date(str);
...
It's not the most optimal... it's designed to show you how to change it yourself. Maybe you could add a second parameter to the function so it can handle a variable input format.

Anyway, hope this works for you...
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thankyou - that works perfectly.

I am now going to try to work out how to do it the other way round and work out the 'calendar' day from the Julian date.

[rant]I think I am starting to understand, but my trouble is after I complete this job I probably won't use Javascript again for a while in which time I will forget everything I have learned... That is the problem with muliti tasking / skilling, it is difficult to become an 'expert' in any of the things you do [/rant]
 
I hear you Simon... it's tough if you are constantly shifting your skills in a job.

The ability to describe a problem concisely is more valuable than knowing specific syntax (imho). So when you (finally) return to Javascript tasks... just do a good job describing your problem (as you did in this case) and hopefully you'll have no problems getting it sorted.

Regarding the last part of your initial question, you can put the result into a textbox (rather than an alert) using something like this:
Code:
<body>
<input type="text" id="txt" /><br>
<input type="button" value="get julian date" onclick="[!]document.getElementById('txt').value = [/!]convertDate(document.getElementById('txt').value)" />
</body>

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks again

That works great. I then wanted to put the date in a new text box so i added

Code:
<input name="text" type="text" id="txt" />
    </div></td>
    <td><input name="button" type="button" onclick="document.getElementById('txt').value = convertDate(document.getElementById('[COLOR=#ff0000]
answer[/color]').value)" value="get julian date" /></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><div align="right">
      [COLOR=#ff0000]<input name="text" type="text" id="answer"  />[/color]

this has stopped it working, and it now returns NaN in the original 'txt' box.

again sorry to be a pain, but you know what i have done wrong ?
 
Thanks Jeff, but I am not quite sure what you mean
 
In the top of the "convertDate" function, right after this line:

Code:
function convertDate(str) {

put an alert to output the value of the "str" parameter:

Code:
alert(str);

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan.

This is my code for the whole page :

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>
<MMString:LoadString id="insertbar/formsCheckbox" />
</title>
<style type="text/css">
<!--
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style2 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
}
-->
</style>
<!-- Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved. -->
</head>

<script type="text/javascript">

//set an array with day counts for all months
var dayCount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function convertDate(str) {
alert(str);
    var str_mm = str.split("/")[0];
    var str_dd = str.split("/")[1];
    var str_yy = str.split("/")[2];
  str = str_dd + "/" + str_mm + "/" + str_yy;
   //convert passed string to date object
   var dte = new Date(str);
   //initialize date variable
   var julianDate = 0;
   //add days for previous months
   for (i = 0; i < dte.getMonth(); i++) {
      julianDate += dayCount[i];
   }
   //add days of the current month
   julianDate += dte.getDate();
   //check for leap year
   if (dte.getFullYear() % 4 == 0 && dte.getMonth() > 1) {
      julianDate++;
   }
   //alert("Julian Date: " + julianDate);
   return julianDate;
}

</script>

<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width="516" border="0" align="center">
  <tr>
    <td width="103"><img src="JC/Images/PNC web.jpg" alt="PNC_Logo" width="100" height="100" align="middle" /></td>
    <td width="41">&nbsp;</td>
    <td width="354"><div align="center"><img src="JC/Images/Title.jpg" alt="PNC : Julian Date Converter" width="263" height="16" align="middle" /></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><div align="center" class="style1">This application allows you to work out the Julian Date from the calendar date </div></td>
  </tr>
  <tr>
    <td colspan="3" class="style1"><div align="center">Enter a date in the text box below (DD/MM/YY) and click the button </div></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><div align="right">
      <input name="text" type="text" id="txt" />
    </div></td>
    <td><input name="button" type="button" onclick="document.getElementById('txt').value = convertDate(document.getElementById('txt').value)" value="get julian date" /></td>
  </tr>
  <tr>
    <td colspan="2"><div align="right">
      <input name="text" type="text" id="answer"  />
    </div></td>
    <td class="style1">Julian Date </td>
  </tr>
</table>
<p><br />
</p>
</body>

</html>

When the user clicks the 'get julian date' button, i want the function to run and the output to appear in the 'answer' text box. At the moment the answer stays in the 'txt' text box.
The alert just shows the date you have just entered.

Do you know where I have gone wrong ?

Cheers
 
Aaah - you were changing the wrong occurrence of "txt" to "answer". You changed the second, you need to change the first. So... this:

Code:
document.getElementById('txt').value = convertDate(document.getElementById('txt').value)

should be:

Code:
document.getElementById('answer').value = convertDate(document.getElementById('txt').value);

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
THANKYOU !!!!!!!!

I have tried everything i could think of, but that seems so obvious now ! THANKYOU THANKYOU THANKYOU
 
OK - I am stuck again already... I know i am pushing my luck here, but if anyone can help....

I want to convert a julian date back to a normal date (the opposite of the above page).

I want the user to input a three digit code (the day number, eg 001 would mean 1st january). I could then have a check box for the user to tick if the year thay are thinking of is a leap year)
I have created a LeapYear daycount that would be used if the box is ticked.
I then want the date to appear in a new text box (minus the year)

 
Simon,

You might want to start that one in a new thread, but reference this thread by pasting it's code (thread216-1191792) into the new post.

A lot of people avoid threads that already have lots of answers.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top