六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 483|回复: 0

【转】JS 时间日期控件

[复制链接]

升级  28%

82

主题

82

主题

82

主题

举人

Rank: 3Rank: 3

积分
284
 楼主| 发表于 2013-2-8 00:57:12 | 显示全部楼层 |阅读模式
<html><body>  <script>function isTime(str){   var a = str.match(/^(\d{0,2}):(\d{0,2}):(\d{0,2})$/);   if (a == null) return false;   if (a[1]>=24 || a[2]>=60 || a[3]>=60) return false;   return true;}function isDateTime(str){   var a = str.match(/^(\d{0,4})-(\d{0,2})-(\d{0,2}) (\d{0,2}):(\d{0,2}):(\d{0,2})$/);   if (a == null) return false;   if ( a[2]>=13 || a[3]>=32 || a[4]>=24 || a[5]>=60 || a[6]>=60) return false;   return true;}function isDate(str){   var a = str.match(/^(\d{0,4})-(\d{0,2})-(\d{0,2})$/);   if (a == null) return false;   if ( a[2]>=13 || a[3]>=32 || a[4]>=24) return false;   return true;}function validate(obj,type){var range=obj.createTextRange(); var text = range.text;var selrange = document.selection.createRange();var seltext = selrange.text;var startpos = 0,endpos = 0;while(selrange.compareEndPoints("StartToStart",range)>0){    selrange.moveStart("character",-1);     startpos ++;}while(selrange.compareEndPoints("EndToStart",range)>0){    selrange.moveEnd("character",-1);     endpos ++;}if(event.keyCode>=48){    var keytext = String.fromCharCode(event.keyCode);   text = text.substring(0,startpos) + keytext + text.substring(endpos,text.length);}else if(event.keyCode == 46){//delete   if(startpos == endpos)text = text.substring(0,startpos) + text.substring(startpos+1,text.length);   else text = text.substring(0,startpos) + text.substring(endpos,text.length);}else if(event.keyCode == 8){   if(startpos == endpos)text = text.substring(0,startpos-1) + text.substring(startpos,text.length);   else text = text.substring(0,startpos) + text.substring(endpos,text.length);}if(event.keyCode == 45){   event.returnValue = false;   return;}var valid;switch(type){   case 1:valid = isDate(text);break;   case 2:valid = isTime(text);break;   case 3:valid = isDateTime(text);break;   default:valid = false;}if(!valid){event.returnValue = false;}}</script><script>function PopupCalendar(InstanceName){///Global Tagthis.instanceName=InstanceName;///Propertiesthis.separator="-"this.oBtnTodayTitle="Today"this.oBtnCancelTitle="Cancel"this.weekDaySting=new Array("S","M","T","W","T","F","S");this.monthSting=new Array("January","February","March","April","May","June","July","August","September","October","November","December");this.Width=200;this.currDate=new Date();this.today=new Date();this.startYear=1950;this.endYear=2010;///Cssthis.divBorderCss="1px solid #BCD0DE";this.tableBorderColor="#CCCCCC"///Methodthis.Init=CalendarInit;this.Fill=CalendarFill;this.Refresh=CalendarRefresh;this.Restore=CalendarRestore;///HTMLObjectthis.oTaget=null;this.oPreviousCell=null;this.sDIVID=InstanceName+"oDiv";this.sTABLEID=InstanceName+"oTable";this.sMONTHID=InstanceName+"oMonth";this.sYEARID=InstanceName+"oYear";}function CalendarInit()     ///Create panel{var sMonth,sYearsMonth=this.currDate.getMonth();sYear=this.currDate.getYear();htmlAll="<div id='"+this.sDIVID+"' style='display:none;position:absolute;width:"+this.Width+";border:"+this.divBorderCss+";padding:2px;background-color:#FFFFFF'>";htmlAll+="<div align='center'>";/// MonthhtmloMonth="<select id='"+this.sMONTHID+"' onchange=CalendarMonthChange("+this.instanceName+") style='width:50%'>";for(i=0;i<12;i++){      htmloMonth+="<option value='"+i+"'>"+this.monthSting[i]+"</option>";}htmloMonth+="</select>";/// YearhtmloYear="<select id='"+this.sYEARID+"' onchange=CalendarYearChange("+this.instanceName+") style='width:50%'>";for(i=this.startYear;i<=this.endYear;i++){   htmloYear+="<option value='"+i+"'>"+i+"</option>";}htmloYear+="</select></div>";/// DayhtmloDayTable="<table id='"+this.sTABLEID+"' width='100%' border=0 cellpadding=0 cellspacing=1 bgcolor='"+this.tableBorderColor+"'>";htmloDayTable+="<tbody bgcolor='#ffffff'style='font-size:13px'>";for(i=0;i<=6;i++){   if(i==0)    htmloDayTable+="<tr bgcolor='#98B8CD'>";   else    htmloDayTable+="<tr>";   for(j=0;j<7;j++)   {    if(i==0)    {     htmloDayTable+="<td height='20' align='center' valign='middle' style='cursor:hand'>";     htmloDayTable+=this.weekDaySting[j]+"</td>"    }    else    {     htmloDayTable+="<td height='20' align='center' valign='middle' style='cursor:hand'";     htmloDayTable+=" onmouseover=CalendarCellsMsOver("+this.instanceName+")";     htmloDayTable+=" onmouseout=CalendarCellsMsOut("+this.instanceName+")";     htmloDayTable+=" onclick=CalendarCellsClick(this,"+this.instanceName+")>";     htmloDayTable+=" </td>"    }   }   htmloDayTable+="</tr>"; }htmloDayTable+="</tbody></table>";/// Today ButtonhtmloButton="<div align='center' style='padding:3px'>"htmloButton+="<button style='width:40%;border:1px solid #BCD0DE;background-color:#eeeeee;cursor:hand'"htmloButton+=" onclick=CalendarTodayClick("+this.instanceName+")>"+this.oBtnTodayTitle+"</button> "htmloButton+="<button style='width:40%;border:1px solid #BCD0DE;background-color:#eeeeee;cursor:hand'"htmloButton+=" onclick=CalendarCancel("+this.instanceName+")>"+this.oBtnCancelTitle+"</button> "htmloButton+="</div>"/// AllhtmlAll=htmlAll+htmloMonth+htmloYear+htmloDayTable+htmloButton+"</div>";document.write(htmlAll);this.Fill(); }function CalendarFill()    ///{var sMonth,sYear,sWeekDay,sToday,oTable,currRow,MaxDay,sDaySn,sIndex,rowIndex,cellIndex,oSelectMonth,oSelectYearsMonth=this.currDate.getMonth();sYear=this.currDate.getYear();sWeekDay=(new Date(sYear,sMonth,1)).getDay();sToday=this.currDate.getDate();oTable=document.all[this.sTABLEID];currRow=oTable.rows[1];MaxDay=CalendarGetMaxDay(sYear,sMonth);oSelectMonth=document.all[this.sMONTHID]oSelectMonth.selectedIndex=sMonth;oSelectYear=document.all[this.sYEARID]for(i=0;i<oSelectYear.length;i++){   if(parseInt(oSelectYear.options[i].value)==sYear)oSelectYear.selectedIndex=i;}////for(sDaySn=1,sIndex=sWeekDay;sIndex<=6;sDaySn++,sIndex++){   if(sDaySn==sToday)   {    currRow.cells[sIndex].innerHTML="<font color=red><i><b>"+sDaySn+"</b></i></font>";    this.oPreviousCell=currRow.cells[sIndex];   }   else   {    currRow.cells[sIndex].innerHTML=sDaySn;    currRow.cells[sIndex].style.color="#666666";    }   CalendarCellSetCss(0,currRow.cells[sIndex]);}for(rowIndex=2;rowIndex<=6;rowIndex++){   if(sDaySn>MaxDay)break;   currRow=oTable.rows[rowIndex];   for(cellIndex=0;cellIndex<currRow.cells.length;cellIndex++)   {    if(sDaySn==sToday)    {     currRow.cells[cellIndex].innerHTML="<font color=red><i><b>"+sDaySn+"</b></i></font>";     this.oPreviousCell=currRow.cells[cellIndex];    }    else    {     currRow.cells[cellIndex].innerHTML=sDaySn;      currRow.cells[cellIndex].style.color="#666666";     }    CalendarCellSetCss(0,currRow.cells[cellIndex]);    sDaySn++;    if(sDaySn>MaxDay)break;    }}}function CalendarRestore()      /// Clear Data{ var oTableoTable=document.all[this.sTABLEID]for(i=1;i<oTable.rows.length;i++){   for(j=0;j<oTable.rows[i].cells.length;j++)   {    CalendarCellSetCss(0,oTable.rows[i].cells[j]);    oTable.rows[i].cells[j].innerHTML=" ";   }} }function CalendarRefresh(newDate)      ///{this.currDate=newDate;this.Restore(); this.Fill(); }function CalendarCellsMsOver(oInstance)     /// Cell MouseOver{var myCellmyCell=event.srcElement;CalendarCellSetCss(0,oInstance.oPreviousCell);if(myCell){   CalendarCellSetCss(1,myCell);   oInstance.oPreviousCell=myCell;}}function CalendarCellsMsOut(oInstance)     ////// Cell MouseOut{var myCellmyCell=event.srcElement;CalendarCellSetCss(0,myCell); }function CalendarCellsClick(oCell,oInstance){var sDay,sMonth,sYear,newDatesYear=oInstance.currDate.getFullYear();sMonth=oInstance.currDate.getMonth();sDay=oInstance.currDate.getDate();if(oCell.innerText!=" "){   sDay=parseInt(oCell.innerText);   if(sDay!=oInstance.currDate.getDate())   {    newDate=new Date(sYear,sMonth,sDay);    oInstance.Refresh(newDate);   }}sDateString=sYear+oInstance.separator+CalendarDblNum(sMonth+1)+oInstance.separator+CalendarDblNum(sDay);   ///return sDateStringif(oInstance.oTaget.tagName=="INPUT"){   oInstance.oTaget.value=sDateString;}document.all[oInstance.sDIVID].style.display="none";  }function CalendarYearChange(oInstance)     /// Year Change{var sDay,sMonth,sYear,newDatesDay=oInstance.currDate.getDate();sMonth=oInstance.currDate.getMonth();sYear=document.all[oInstance.sYEARID].valuenewDate=new Date(sYear,sMonth,sDay);oInstance.Refresh(newDate);}function CalendarMonthChange(oInstance)     /// Month Change{var sDay,sMonth,sYear,newDatesDay=oInstance.currDate.getDate();sMonth=document.all[oInstance.sMONTHID].valuesYear=oInstance.currDate.getYear();newDate=new Date(sYear,sMonth,sDay);oInstance.Refresh(newDate); }function CalendarTodayClick(oInstance)     /// "Today" button Change{ oInstance.Refresh(new Date());  }function getDateString(oInputSrc,oInstance){if(oInputSrc&&oInstance) {   CalendarDiv=document.all[oInstance.sDIVID];   oInstance.oTaget=oInputSrc;   CalendarDiv.style.pixelLeft=(CalendargetPos(oInputSrc,"Left")+135);   CalendarDiv.style.pixelTop=CalendargetPos(oInputSrc,"Top");   CalendarDiv.style.display=(CalendarDiv.style.display=="none")?"":"none";} }function CalendarCellSetCss(sMode,oCell)    /// Set Cell Css{// sMode// 0: OnMouserOut 1: OnMouseOver if(sMode){   oCell.style.border="1px solid #5589AA";   oCell.style.backgroundColor="#BCD0DE";}else{   oCell.style.border="1px solid #FFFFFF";   oCell.style.backgroundColor="#FFFFFF";} }function CalendarGetMaxDay(nowYear,nowMonth)    /// Get MaxDay of current month{var nextMonth,nextYear,currDate,nextDate,theMaxDaynextMonth=nowMonth+1;if(nextMonth>11){   nextYear=nowYear+1;   nextMonth=0;}else {   nextYear=nowYear; }currDate=new Date(nowYear,nowMonth,1);nextDate=new Date(nextYear,nextMonth,1);theMaxDay=(nextDate-currDate)/(24*60*60*1000);return theMaxDay;}function CalendargetPos(el,ePro)     /// Get Absolute Position{var ePos=0;while(el!=null){     ePos+=el["offset"+ePro];   el=el.offsetParent;}return ePos;}function CalendarDblNum(num){if(num<10)    return "0"+num;else   return num;}function CalendarCancel(oInstance)    ///Cancel{CalendarDiv=document.all[oInstance.sDIVID];CalendarDiv.style.display="none";  }</script><script>var oCalendarChs=new PopupCalendar("oCalendarChs"); //初始化控件时,请给出实例名称:oCalendarChsoCalendarChs.weekDaySting=new Array("日","一","二","三","四","五","六");oCalendarChs.monthSting=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");oCalendarChs.oBtnTodayTitle="今天";oCalendarChs.oBtnCancelTitle="取消";oCalendarChs.Init();</script><input id="date" onkeyDown="validate(this,3)" value="0000-00-00 00:00:00"  > <input onkeyDown="validate(this,1)" value="0000-00-00" onDblClick="getDateString(this,oCalendarChs)" ></body></html>分享到搜狐微博 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表