对象化JS之 --日期选择
2007-12-06 09:39:54 来源:WEB开发网<SCRIPT LANGUAGE=javascript>
onload=function(){
var lodate = new dateSelect();
lodate.setDate(2003-01-01);
}
function dateSelect(aoP){
var loD = document.createElement(SPAN);
if (aoP==null){
document.body.appendChild(loD);
}else{
aoP.appendChild(loD);
}
loD.onSelectDate = function(){};
loD.getDate = function(){
var loChilds = this.all.tags(SELECT);
var lsDate = ;
for (var i=0; i<loChilds.length; i++){
var li = parseFloat(loChilds[i].value);
if (li<10) li=0+li;
lsDate+=li;
if (i<2) lsDate+=-;
if (i==2) lsDate+= ;
if (i>2&&i<loChilds.length-1) lsDate+=:;
}
return lsDate;
}
loD.setDate = function(asDate){
this.innerHTML = ;
var liSpaceYear = 20;
var loNowDate = new Date();
var liYear = loNowDate.getYear();
var liMonth = loNowDate.getMonth()+1;
var liDay = loNowDate.getDate();
var liHour = loNowDate.getHours();
var liMinute = loNowDate.getMinutes();
if (asDate!=null&&asDate!=){
var lxDate = asDate.split( );
if (lxDate.length>0){
var lxUp = lxDate[0].split(-);
if (lxUp.length>0) liYear = parseFloat(lxUp[0]);
if (lxUp.length>1) liMonth = parseFloat(lxUp[1]);
if (lxUp.length>2) liDay = parseFloat(lxUp[2]);
}
if (lxDate.length>1){
var lxDown = lxDate[1].split(:);
if (lxDown.length>0) liHour = parseFloat(lxDown[0]);
if (lxDown.length>1) liMinute = parseFloat(lxDown[1]);
}
}
var loYear = document.createElement(SELECT);
loYear.onchange = new Function(+this.uniqueID+.setDate(+this.uniqueID+.getDate()));
loYOpts = loYear.options;
for (var i=liYear-liSpaceYear; i<liYear+liSpaceYear; i++){
loYOpts[loYOpts.length] = new Option(i,i);
if (i==liYear) loYOpts[loYOpts.length-1].selected=true;
}
this.insertBefore(loYear);
var loFont = document.createElement(FONT);
loFont.innerHTML = 年;
this.insertBefore(loFont);
var loMonth = document.createElement(SELECT);
loMonth.onchange = new Function(+this.uniqueID+.setDate(+this.uniqueID+.getDate()));
loMOpts = loMonth.options;
for (var i=1; i<=12; i++){
loMOpts[loMOpts.length] = new Option(i,i);
if (i==liMonth) loMOpts[loMOpts.length-1].selected=true;
}
this.insertBefore(loMOpts);
var loFont = document.createElement(FONT);
loFont.innerHTML = 月;
this.insertBefore(loFont);
var loDay = document.createElement(SELECT);
loDay.onchange = new Function(+this.uniqueID+.onSelectDate());
loDOpts = loDay.options;
var liMaxDay=getMaxDay(liYear,liMonth);
for (var i=1; i<=liMaxDay; i++){
loDOpts[loDOpts.length] = new Option(i,i);
if (i==liDay) loDOpts[loDOpts.length-1].selected=true;
}
this.insertBefore(loDay);
var loFont = document.createElement(FONT);
loFont.innerHTML = 日;
this.insertBefore(loFont);
var loHour = document.createElement(SELECT);
loHour.onchange = new Function(+this.uniqueID+.onSelectDate());
loHOpts = loHour.options;
for (var i=0; i<=23; i++){
loHOpts[loHOpts.length] = new Option(i,i);
if (i==liHour) loHOpts[loHOpts.length-1].selected=true;
}
this.insertBefore(loHOpts);
var loFont = document.createElement(FONT);
loFont.innerHTML = 时;
this.insertBefore(loFont);
var loMinute = document.createElement(SELECT);
loMinute.onchange = new Function(+this.uniqueID+.onSelectDate());
loMiOpts = loMinute.options;
for (var i=0; i<=59; i++){
loMiOpts[loMiOpts.length] = new Option(i,i);
if (i==liMinute) loMiOpts[loMiOpts.length-1].selected=true;
}
this.insertBefore(loMinute);
var loFont = document.createElement(FONT);
loFont.onchange = new Function(+this.uniqueID+.onSelectDate());
loFont.innerHTML = 分;
this.insertBefore(loFont);
loD.onSelectDate();
function getMaxDay(aiYear,aiMonth){
var loDay = [0,31,28,31,30,31,30,31,31,30,31,30,31];
if (aiYear%4==0) loDay[2] = 29;
return loDay[aiMonth];
}
}
return loD;
}
</SCRIPT>
更多精彩
赞助商链接