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!

create calendar date from day number 1

Status
Not open for further replies.

simonWMC2

Programmer
Aug 5, 2004
161
GB
Hello...

After a lot of help from you guys, I got a page working that converts a date into the Julian day number (day of the year - eg 01/01/06 would be day number 1, 01/02/06 would be day number 32) see (thread216-1191792)

What I want to do now is create a page that does the opposite.
The user can add a day number (eg, 32 then i want the function to work out the date - eg 01/02) There is an obvious problem with the year, as it may be a leap year. To combat this I thought of having a check box that the user wold tick if the year they were thinking of was a leap year. The function could then use a differant var of leap year days.
However, I am not sure of how to begin... can anyone help ?

thankyou
 
ok, I got this far, but i don't really know what i am doing

function convertLydate(strLY) {
strLy = strLy;
var jd = new(strLy);
var calDte = str_dd + "/" + str_mm;
 
Here is one I created earlier [smile]
Code:
<html>
<head>
<title>Julian Date Sample</title>
<script type="text/javascript">
function getDateFromJulian(_data,_leap) {
	//set an array with day counts for all months
	var dayCount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	// convert the number of days into a number
	var days = parseInt(_data,10);

	// some simple error checking of data
	if (days > 366 || days == 366 && !_leap) return('too many days');
	if (days < 1) return('too few days');

	// update the number of days for february if it is a leap year
	if (_leap) dayCount[1] = 29;

	var mm = temp = 0;
	if (days <= 366) {
		while (temp < days) temp += dayCount[mm++];
		temp = temp - dayCount[mm-1];
		dd = days - temp;
		return(dd + '/' + mm);
	}
}

function alertDate() {
	var _days = document.getElementById('dayinput').value;
	var _leap = document.getElementById('leap').checked;
	alert(getDateFromJulian(_days,_leap));
}
</script>
</head>
<body>
<form action="">
	<fieldset>
		<input type="text" id="dayinput"/>
		<input type="checkbox" id="leap"/><label for="leap">Leap year</label>
		<input type="button" onclick="alertDate()" value="Show date"/>
	</fieldset>
</form>
</body>
</html>
Hope this is what you are after!

Cheers,
Jeff

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

What is Javascript? FAQ216-6094
 
This function seems to work, though it does not handle the leap year.

Code:
function ConvertDate(strDate)
{
		var number = parseInt(strDate);
		document.writeln("Data input is " + number);
		var retVal;
		
		var months = new Array(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
		var marker;
		var sum = 0;
		
		for(marker = 0; marker < months.length; marker++)
		{
						
			if(number <= months[marker] )
			{
				var d = number - sum;
				var m = marker + 1;
				if(d < 10)
				{
					retVal = "0" + d;
				}
				else
				{
					retVal = "" + d;
				}
				if(m < 10)
				{
					retVal = retVal + "/0" + m;
				}
				else
				{
					retVal = retVal + "/" + m;
				}
				
				break;
			}
			
			sum = months[marker];
		}
		
		return retVal;
		
}


Let me know if that helps at all.

Nick
 
Sorry Jeff,
Didn't see that you had already replied to this.

 
No stress... the more different ways we see something solved, the more we learn. It's really important to remember that there are usually many ways to code a solution... and each has certain merits.

Cheers,
Jeff

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

What is Javascript? FAQ216-6094
 
Thanks Guys !!!

I have included Jeffs code in my page and it works great.

However, it would be perfect if I could have the 'result' appear in a text box ("answerLy") instead of an alert.

I guess I can do this by ammeding this function
Code:
}
function alertDate() {
    var _days = document.getElementById('dayinput').value;
    var _leap = document.getElementById('leap').checked;
    alert(getDateFromJulian(_days,_leap));
}

but i am not sure how...

 
that is what i thought, but it was slightly differant. But i did manage to work it out myself (woooHooo !)

I just took out the alert and added

Code:
document.getElementById('answerly').value = getDateFromJulian(_days,_leap);

I might get the hang of this javascript thing afterall !!!!

thanks for all your help guys. I hope one day I can repay the favour !

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top