// a simple calendar // 2007 ryan yoosefi // // requires a div with id "calendar" and an input field with id "date" // call up the initial calendar without arguments: placeCalendar() // retrieve an array of the current set date in the date field // returns Array(month,day,year) function getCurrentDate(){ return document.getElementById('date').value.split("/"); } // places the date in the "date" input field when calendar is clicked // and highlights that day on the calendar function s(month,day,year){ currentDate = getCurrentDate(); try{document.getElementById(currentDate[1]).className='day';} catch(e){}//incase new month has more days than last document.getElementById('date').value = month+"/"+day+"/"+year; var sweManad; var sweDay; if(String(month).length == 1) { sweManad = "0" + String(month); } else { sweManad = String(month); } if(String(day).length == 1) { sweDay = "0" + String(day); } else { sweDay = String(day); } document.getElementById('dateSwe').value = year+"-"+sweManad+"-"+sweDay; document.getElementById(day).className='highlighted'; } // navigate the calendar left/right (backward/forward) function calendarNav(direction){ switch (direction){ case "left":{ if(d.getMonth()=="0"){newMonth=11;newYear=d.getFullYear()-1;} else{newMonth=d.getMonth()-1;newYear=d.getFullYear()} break; } case "right":{ if(d.getMonth()==11){newMonth=0;newYear=d.getFullYear()+1;} else{newMonth=d.getMonth()+1;newYear=d.getFullYear();} break; } default:{ // do nothing. basically, if you manually set the date field // you can use calendarNav() with no args to refresh what day // is highlighted. // this however is only useful if you are in the same month. // to display a custom calendar you should really use // placeCalendar(month,year,day); break; } } // refresh the calendar placeCalendar(newMonth,newYear); // preserve date highlighting currentDate = getCurrentDate(); if(typeof currentDate[1]!="undefined"){ if ((currentDate[0]==newMonth+1)&&(currentDate[2]==newYear)){ s(newMonth+1,currentDate[1],newYear); } } } // write/refresh calendar to the calendar div // all args optnl: // month - sets month of calendar; 0=january // year - sets year // day - highlight a given day function placeCalendar(month,year,day){ // main date object d=new Date(); // load the date object with provided vars if any if(typeof month!="undefined")d.setMonth(month); if(typeof year!="undefined")d.setFullYear(year); if(typeof day!="undefined"){d.setDate(day);highlightDay=day;} // use this copy of the date object for local manipulation dx=d; // bring date vars to local ones month=d.getMonth(); year=d.getFullYear(); day=d.getDate(); // configure months and weeks days=new Array('Sö','Må','Ti','On','To','Fr','Lö'); months=new Array('Januari','Februari','Mars','April','Maj','Juni','Juli','Augusti','September','Oktober','November','December'); mlen=new Array(31,28,31,30,31,30,31,31,30,31,30,31); if(d.getFullYear()%4==0){mlen[1]=29;} //leapyear // calendar start html cal=""; cal+=""; cal+=""; cal+=""; for(i=0;i<=6;i++){cal+="";} cal+=""; // iterate the days and insert them for(i=1;i<=mlen[dx.getMonth()];i++){ dx.setDate(i); iday=dx.getDay(); // starting at appropriate day of the week if(i==1){ for(j=0;j"; cal+=""; if((iday==6)||(i==days[month]))cal+=""; } // close the calendar table cal+="
<<"+months[month]+" "+year+">>
"+days[i]+"
"+i+"
"; // write out the calendar to the div document.getElementById('calendar').innerHTML=cal; // if a day was specified as an argument, highlight it if (typeof highlightDay!="undefined")s(month+1,highlightDay,year); // and by default, if there currently is no selected date at all // then select the current day currentDate = getCurrentDate(); if (typeof currentDate[1]=="undefined")s(month+1,day,year); }