PDA

View Full Version : Adjusting date selector script



estheridb
09-23-2007, 01:42 PM
On the net I found a date selector script (unfortunately the author isn't stated). It is nearly perfect for my goal. It generates the weekday, day, month and year. Default it displays the current date. By changing the date, the weekday changes automatically also. The script displays the correct numbers within each month, even taking in account leap years.

I would like to put the script in a form, and the selected date must be a required field. But all variables are seperate fields, so that is a problem. (In most cases 1 field being changed is enough.) Further more I don't know how the script can see that a choice has been made. (When people want to choose the default displayed date, they haven't changed anything.) Normally I display the default Choose... I would like to change the script in such a manner that day en month get the default value Choose (the year displays the current year en de weekday displays blank) and when choosing day and month the weekday is automaticly chosen correctly. It is required to change Choose. It would be really perfect if people cannot select the current date. I've been trying for hours to change this, but I don't have enough knowledge of javascript to get this right. (I'm a newbie.)

I can imagine this being a rather complicated adjustment. My second best option would be not to make the field required, but to display tomorrow's date as default. (That being the only adjustment in the script.) I think that adjustment probably is much simpler. (Although I can't make this happen either :( )

All input on either one of the solutions is much appreciated! Thanking you in advance.

This is the code:


<script language="JavaScript1.1">
<!--
var min_year = 2007;
var max_year = 2009;

var weekday_showing = true;

var dayofweek_returned_as_number = false;

var month_returned_as_number = false;

if (min_year <= 400)
alert(".");

function changeDays(numb,date_form) {
mth = date_form.month.selectedIndex;
sel = date_form.year.selectedIndex;
yr = date_form.year.options[sel].text;
if (numb != 1) {
numDays = numDaysIn(mth,yr);
date_form.day.options.length = numDays;
for (i=27;i<numDays;i++) {
date_form.day.options[i].text = i+1;
}
}
day = date_form.day.selectedIndex+1;
if (weekday_showing)
date_form.dayofweek.selectedIndex = getWeekDay(mth,day,yr);
}
function numDaysIn(mth,yr) {
if (mth==3 || mth==5 || mth==8 || mth==10) return 30;
else if ((mth==1) && leapYear(yr)) return 29;
else if (mth==1) return 28;
else return 31;
}
function leapYear(yr) {
if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0)
return true;
else
return false;
}
function arr() {
this.length=arr.arguments.length;
for (n=0;n<arr.arguments.length;n++) {
this[n] = arr.arguments[n];
}
}

weekdays = new arr("Zondag","Maandag","Dinsdag","Gesloten!",
"Donderdag","Vrijdag","Zaterdag");

months = new arr("Januari","Februari","Maart","April","Mei",
"Juni","Juli","Augustus","September","Oktober","November","December");
var cur = new Date();

function getWeekDay(mth,day,yr) {
first_day = firstDayOfYear(yr);
for (num=0;num<mth;num++) {
first_day += numDaysIn(num,yr);
}
first_day += day-1;
return first_day%7;
}
function firstDayOfYear(yr) {
diff = yr - 401;
return parseInt((1 + diff + (diff / 4) - (diff / 100) + (diff / 400)) % 7);
}
function getFullYear(d) { // d is af date object
yr = d.getYear();
if (yr < 1000)
yr+=1900;
return yr;
}

if (weekday_showing) {
document.write("<select name=weekdag size=1>");
for (i=0;i<7;i++)
document.write("<option"+(dayofweek_returned_as_number?" value="+i:"")
+(cur.getDay()==i?" selected":"")+">"+weekdays[i]+"\n");
document.write("</select>");
}


document.write("<select name=maand "
+ "onChange='changeDays(0,this.form)' size=1>");
for (i=0;i<12;i++)
document.write("<option"+(month_returned_as_number?" value="+i:"")
+(cur.getMonth()==i?" selected":"")+">"+months[i]+"\n");


document.write("</select><select name=dag size=1 "
+ "onChange='changeDays(1,this.form)'>\n");
for (i=1;i<=numDaysIn(cur.getMonth(),getFullYear(cur));i++)
document.write("<option"+(cur.getDate()==i?" selected":"")+">"+i+"\n");


document.write("</select><select name=jaar "
+ "onChange='changeDays(0,this.form)' size=1>\n");
for (i=min_year;i<max_year;i++)
document.write("<option"+(getFullYear(cur)==i?" selected":"")+">"+i+"\n");
document.write("</select>");
// -->
</script>

estheridb
09-24-2007, 05:51 PM
I already found an answer for my second best solution: after

var cur = new Date();

I put in the code:

cur.setDate(cur.getDate()+1);

So that leaves only my preferred solution open. Does anyone have an input?