Click to See Complete Forum and Search --> : Calendar pull down menu adjustment
Who can help me with the following problem
Ik want to use the following script: http://javascript.internet.com/calendars/date-menu.html
But i want a little adjustment: who can help to change the script so that is just shows the fridays (now it's showing all days of the month):confused:
Thanks for any hints and/or tips
Hinf
olerag
01-06-2004, 04:01 PM
A quick scan of the code and this should work..
In their "populate()" function, at the last "for" loop,
termination of the iteration would change from the amount of
days in the month to, possibly, the length of a new Array
you'd make that only contained the number of Friday's in that
month. This array population would be performed prior to the
first for iteration of that function.
How to obtain "Friday's" values? Iterate thru the var they are
using to store the selected date (timeB) and evaluate
which of the days in the month are a Friday.
To derive the days in a month: timeB.getDate(). This is
how many iterations will be performed (minus 1).
To derive the days within a week: timeB.getDay() (Friday,
I believe, will return a 5). Only values that are Fridays
will be placed in the array.
Once the array is filled, the "day" options can be cleared and
re-populated.
Let me know if you'd like a code sample (tommorow) but
I'm going home now.
if you can give me a sample code it would be great! i'm new to scripting. i can read the script and modify little parts, but writing is still a dream!!!
Thanks
Hinf
olerag
01-07-2004, 09:59 AM
I added a new <select> to show Fridays for that month and
it appears to the right of the "days" <select> object. Simply
replace their "populate()" function with the one below and
add the HTML to see the results.
And I believe they want their name attached to this so...
<script type="text/javascript">
<!-- Originally designed by Javascript Source (http://javascriptsource.com -->
function populate(objForm,selectIndex) {
timeA = new Date(objForm.year.options[objForm.year.selectedIndex].text, objForm.month.options[objForm.month.selectedIndex].value,1);
timeDifference = timeA - 86400000;
timeB = new Date(timeDifference);
var daysInMonth = timeB.getDate();
for (var i = 0; i < objForm.day.length; i++) {
objForm.day.options[0] = null;
}
for (var i = 0; i < daysInMonth; i++) {
objForm.day.options[i] = new Option(i+1);
}
var fri = new Array();
var count = 0;
var tempDate;
for (var i=0; i<daysInMonth; i++) {
tempDate = new Date(objForm.year.options[objForm.year.selectedIndex].text, objForm.month.options[objForm.month.selectedIndex].value,i);
if (tempDate.getDay()+1 == 2) {
fri[count] = i;
count++;
}
}
for (var i = 0; i < objForm.friday.length; i++) {
objForm.friday.options[0] = null;
}
for (var i = 0; i < fri.length; i++) {
objForm.friday.options[i] = new Option(fri[i]);
}
document.f1.day.options[0].selected = true;
document.f1.friday.options[0].selected = true;
}
<select name=friday>
<option> </option>
</select>
Sorry - edited slightly. If you see this before you cut/paste
you've got the up-to-date sample.
Looking good!
can you explain why this works????
Found a little problem, the script works for january march may ect, but not for february june.....
any idea why?
THANKS
Hinf
olerag
01-07-2004, 04:01 PM
Dammit, and sorry. Try this instead. And yes, that other
function (that preloads the years) is totally unnecessary
so its not included here.
<html>
<head>
<script type="text/javascript">
function populate(objForm,selectIndex) {
timeA = new Date(objForm.year.options[objForm.year.selectedIndex].value, objForm.month.options[objForm.month.selectedIndex].value,1);
timeDifference = timeA - 86400000;
timeB = new Date(timeDifference);
var daysInMonth = timeB.getDate();
for (var i = 0; i < objForm.day.length; i++) {
objForm.day.options[0] = null;
}
for (var i = 0; i < daysInMonth; i++) {
objForm.day.options[i] = new Option(i+1);
}
var fri = new Array();
var count = 0;
for (var i=0; i<daysInMonth; i++) {
tempDate = new Date(objForm.year.options[objForm.year.selectedIndex].value, objForm.month.options[objForm.month.selectedIndex].value-1,i);
if (tempDate.getDay() == 4) {
fri[count] = i+1;
count++;
}
}
for (var i = 0; i < objForm.friday.length; i++) {
objForm.friday.options[0] = null;
}
for (var i = 0; i < fri.length; i++) {
objForm.friday.options[i] = new Option(fri[i]);
}
document.f1.day.options[0].selected = true;
document.f1.friday.options[0].selected = true;
}
</script>
</head>
<body>
<center>
<form name="f1">
<table border="0">
<tr>
<td align=center>
<select name=year onChange="populate(this.form,this.form.month.selectedIndex);">
<option value=1997>1997</option>
<option value=1998>1998</option>
<option value=1999>1999</option>
<option value=2000>2000</option>
<option value=2001>2001</option>
<option value=2002>2002</option>
<option value=2003>2003</option>
<option value=2004 selected>2004</option>
<option value=2005>2005</option>
<option value=2006>2006</option>
</select>
<select name=month onChange="populate(this.form,this.selectedIndex);">
<option value=01>January</option>
<option value=02>February</option>
<option value=03>March</option>
<option value=04>April</option>
<option value=05>May</option>
<option value=06>June</option>
<option value=07>July</option>
<option value=08>August</option>
<option value=09>September</option>
<option value=10>October</option>
<option value=11>November</option>
<option value=12>December</option>
</select>
<select name=day>
<option> </option>
<option> </option>
<option> </option>
<option> </option>
<option> </option>
<option> </option>
<option> </option>
</select>
<select name=friday>
<option> </option>
</select>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Oh, you wanted an explanation..
1. I removed the "populateYear()" function and simply
placed 4-digit years in the <select> options.
2. The populate function:
- Obtains a date and substracts by 1 day (0 based days);
The result of the actual date is stored in "timeB".
- The "daysInMonth" var stores the qty of days in the
date and iterates thru this value to populate the "Days"
<select> tag, after it clears it of previous values.
- What I add was an iteration of the "daysInMonth" and,
if that particular day is a Friday, its placed in a new Array,
called "fri".
- I remove one day from the initial selection when the date is
calculated instead of issueing an additional substracting
routine, as the original authors did.
- Once this new array is populated, the values are placed
in the new <select> tag, "friday".
3. The "populate()" function is called anytime a new
year or month is selected.
This sample could be used to pick the specific year, month,
and day and obtain a JS date for subsequent processing.
you just made me a happy man!!!!
i made some small changes to remove the .day. and populate the .friday. onload.
Thans for your help!
Hinf
My "final script" for those who also need it:
<html>
<head>
<script type="text/javascript">
<!-- Special thanks to olerag -->
<!-- Supreme Master of the Web -->
function populate(objForm,selectIndex) {
timeA = new Date(objForm.year.options[objForm.year.selectedIndex].value, objForm.month.options[objForm.month.selectedIndex].value,1);
timeDifference = timeA - 86400000;
timeB = new Date(timeDifference);
var daysInMonth = timeB.getDate();
for (var i = 0; i < objForm.friday.length; i++) {
objForm.friday.options[0] = null;
}
for (var i = 0; i < daysInMonth; i++) {
}
var fri = new Array();
var count = 0;
for (var i=0; i<daysInMonth; i++) {
tempDate = new Date(objForm.year.options[objForm.year.selectedIndex].value, objForm.month.options[objForm.month.selectedIndex].value-1,i);
if (tempDate.getDay() == 4) {
fri[count] = i+1;
count++;
}
}
for (var i = 0; i < objForm.friday.length; i++) {
objForm.friday.options[0] = null;
}
for (var i = 0; i < fri.length; i++) {
objForm.friday.options[i] = new Option(fri[i]);
}
document.f1.friday.options[0].selected = true;
}
</script>
</head>
<body onload="populate(this.f1,this.f1.month.selectedIndex)">
<center>
<form name="f1">
<table border="0">
<tr>
<td align=center>
<select name=year onChange="populate(this.form,this.form.month.selectedIndex);">
<option value=2004 selected>2004</option>
<option value=2005>2005</option>
<option value=2006>2006</option>
</select>
<select name=month onChange="populate(this.form,this.selectedIndex);">
<option value=01>January</option>
<option value=02>February</option>
<option value=03>March</option>
<option value=04>April</option>
<option value=05>May</option>
<option value=06>June</option>
<option value=07>July</option>
<option value=08>August</option>
<option value=09>September</option>
<option value=10>October</option>
<option value=11>November</option>
<option value=12>December</option>
</select>
<select name=friday>
<option> </option>
</select>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
olerag
01-08-2004, 08:29 AM
YW - You must want to make sure you don't miss a pay
day.
Oh, why don't you remove that second "for" iteration since
its doing nothing anyway???
Thats the part i still need to learn..... :)
Hinf