This simple javascript method allows you to dynamically create a drop down box, or select element as they are often called, with dates going back as far as you need them to from the current date. All you need to do is pass the number of days you'd like the select to go back -- in this case seven days -- and add a div element with an id of mContainer that the function will set the innerHTML of. The option object values are formatted as YYYYMD.
Update: Per a user's request, this method has been modifed to accept an additional argument that tells the script to count forward (0) or backwards (1).
function buildDateDropDown(days,direction) { // 1 = backwards, 0 = forwards // the var "previous" now means the max future or max past date. i'm much too lazy to change it. var dayLong = 86400000; var current = Date.parse(new Date()); var previous = direction?current - (dayLong*days):current + (dayLong*days); previous = new Date(previous); months = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); mHTML = "<select name=\"mDate\">"; do { direction?current-=dayLong:current+=dayLong prvDateObj = new Date(current); prvMonth = months[prvDateObj.getMonth()]; prvDay = days[prvDateObj.getDay()]; prvDate = prvDateObj.getDate(); prvYear = prvDateObj.getFullYear(); vDate = prvYear + "" + prvDateObj.getMonth() + "" + prvDateObj.getDate(); mHTML+= "<option value=" + vDate + ">" + prvDay + ", " + prvMonth + " " + prvDate + " " + prvYear condition = direction?prvDateObj>previous:prvDateObj<previous; } while(condition); mHTML+="</select>"; document.getElementById("mContainer").innerHTML = mHTML; }
Dynamic Date Dropdown v2.0 Last revision: 02.25.2004 steve@slayeroffice.com http://www.slayeroffice.com |