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 1

Status
Not open for further replies.

simonWMC2

Programmer
Aug 5, 2004
161
GB
Hello... I already asked this in the ULTRA DEV forum, so apologies if you are reading it for the second time...
But I is still stuck so...
------------------------------------------------------------

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 !
 
This should do exactly what you're looking for. I've commented everything so that it should be easy for you to understand what the code does.
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>

Now, for the code that you want to be backwards compatible (i.e. - they insert the julian date and you kick back the month/day/year), there's a flaw in the logic of how that would work. For example, if I enter a value of 60, should it return march 1st, or feb 29th? Approaching this problem backwards would require them to supply what year they are trying to get the date for - otherwise you have no clue if it's a leap year or not.

That being the case, if you decide to make that part of the specs then it should be easy enough to modify my code to make it work both ways.

Additionally, since this isn't a dreamweaver question at all, you might want to direct all future javascript questions to the javascript forum: forum216

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Thankyou...

Sorry..of course it is the wrong forum...
Was not intentional I was just being a bit thick !!!

Thanks again, this is perfect and really helps. I will now try and crack it so it works both ways
 
It's not a big deal, I spend most of my time in the javascript forum and peek in here occasionally. And although many people here might be well versed in javascript, you'd likely have more luck asking in the forum where people are expected to answer javascript questions. Your question could have sat here unanswered for days whereas in the javascript forum it would have likely been answered in less than 30 mins. Anyway, all's well that ends well [smile]

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top