...

View Full Version : Self updating date drop down box



simbarashe
12-20-2006, 04:05 AM
I'm trying to make a drop down box for the date mont and year for my website. However the days in february are 28 or 29 but the other months have either 30 or 31. How can i make a script that enables the date drop down box to change to the appropriate number of days,say if february was selected or if december was selected also considering leaps years and stuff

Ancora
12-20-2006, 01:00 PM
simbarashe:

Start by writing "please" and "thank you."

simbarashe
12-21-2006, 07:26 AM
sorry about that I was rushing to work and a bit frustrated coz I had tried everything I couold think of. I'm really sorry, may you please advise me on how I can go abput solving my problem

Thank you

david_kw
12-21-2006, 08:23 AM
Try this example for finding the number of days in a month. There may be a better way but I don't know it.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Calc Days in Month</title>
<script type="text/javascript">
function doIt(s) {
var adiv = document.getElementById("adiv");
adiv.innerHTML = s;
}

function calcDays(mon, year) {
var dt = new Date();
var m = parseInt(mon) - 1; // months for Date() are from 0 to 11
var y = parseInt(year);

for (var i = 1; i < 5; i++) {
dt.setFullYear(y, m, 28 + i); // assume each month has at least 28 days
if (dt.getMonth() != m) {
break;
}
}

return(28 + i - 1);
}
</script>
</head>
<body>
<div>
<form name="f" action="#" onsubmit="return(false);">
<label>Month <input type="text" name="m" value="12"></label>
<br />
<label>Year <input type="text" name="y" value="2006"></label>
</form>
<button onclick="doIt(calcDays(document.f.m.value, document.f.y.value));">Calc Num Days</button>
<br />
<div id="adiv"> </div>
</div>
</body>
</html>


david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum