日期:2014-05-16 浏览次数:20464 次
/** * 日期的控件YYYY-MM-DD * 使用实例:onkeyup="OnKeyDownTime(id,event)" */ var T = new Array("Y","Y","Y","Y","M","M","D","D");//显示的日期 var D = new Array("Y","Y","Y","Y","M","M","D","D");//初始化格式 var index = 0;//格式索引 function initTimePage(id) {//初始化页面 index = 0; for(var i=0;i<T.length;i++) { T[i]=D[i]; } outputDateToTimeTxt(id); } function OnKeyDownTime(id,event) {//键盘监听 if(event.keyCode > 47 && event.keyCode < 58) { T[index] = String.fromCharCode(event.keyCode); if(index < T.length-1) { index++; } } if(event.keyCode == 37) { T[index] = D[index]; if(index > 0) { index--; } } outputDateToTimeTxt(id); } function outputDateToTimeTxt(id) {//在指定位置显示日期 document.getElementById(id).value = T[0]+T[1]+T[2]+T[3]+"-"+T[4]+T[5]+"-"+T[6]+T[7];//显示日期 var d = document.getElementById(id).createTextRange(); if(index > 5) { d.moveStart("character",index+2); d.moveEnd("character",-7+index); }else if(index > 3) { d.moveStart("character",index+1); d.moveEnd("character",-8+index); }else { d.moveStart("character",index); d.moveEnd("character",-9+index); } d.select(); } /** * YYYY-MM-DD格式是否正确,传入参数为text的value */ function isDataFormatOK(DataValue) {//日期格式是否正确的判断 var returnTarget = false; if(DataValue != null && DataValue.length == 10){ var year = DataValue.substring(0,4); var month = DataValue.substring(5,7); var day = DataValue.substring(8,10); if(isNaN(year+month+day)) { return returnTarget; } if(parseInt(year,10)>0 && parseInt(year,10)<3000) { if(month=="01"||month=="03"||month=="05"||month=="07"||month=="08"||month=="10"||month=="12") { if(parseInt(day,10)>0 && parseInt(day,10)<32) { returnTarget = true; } }else if(month=="04"||month=="06"||month=="09"||month=="11") { if(parseInt(day,10)>0 && parseInt(day,10)<31) { returnTarget = true; } }else if(month=="02") { if((parseInt(year,10)%100==0 && parseInt(year,10)%400==0) || (parseInt(year,10)%100!=0 && parseInt(year,10)%4==0)) { if(parseInt(day,10)>0 && parseInt(day,10)<30) { returnTarget = true; } }else { if(parseInt(day,10)>0 && parseInt(day,10)<29) { returnTarget = true; } } } } } return returnTarget; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * 日期时间的控件YYYY-MM-DD HH:MM * 使用实例:onkeyup="OnKeyDownTime2(id,event)" */ var TT = new Array("Y","Y","Y","Y","M","M","D","D","H","H","M","M");//显示的日期时间 var DD = new Array("Y","Y","Y","Y","M","M","D","D","H","H","M","M");//初始化格式 var index2 = 0;//格式索引 function initTime2Page(id) {//初始化页面 index2 = 0; for(var i=0;i<TT.length;i++) { TT[i]=DD[i]; } outputDateToTimeTxt2(id); } function OnKeyDownTime2(id,event) {//键盘监听 if(event.keyCode > 47 && event.keyCode < 58) { TT[index2] = String.fromCharCode(event.keyCode); if(index2 < TT.length-1) { index2++; } } if(event.keyCode == 37) { TT[index2] = DD[index2]; if(index2 > 0) { index2--; } } outputDateToTimeTxt2(id); } function outputDateToTimeTxt2(id) {//在指定位置显示日期 document.getElementById(id).value = TT[0]+TT[1]+TT[2]+TT[3]+"-"+TT[4]+TT[5]+"-"+TT[6]+TT[7]+" "+TT[8]+TT[9]+":"+TT[10]+TT[11];//显示日期 var d = document.getElementById(id).createTextRange(); if(index2 > 9){ d.moveStart("character",index2+4); d.moveEnd("character",-11+index2); }else if(index2 > 7){ d.moveStart("character",index2+3); d.moveEnd("character",-12+index2); }else if(index2 > 5) { d.moveStart("character",index2+2); d.moveEnd("character",-13+index2); }else if(index2 > 3) { d.moveStart("character",index2+1); d.moveEnd("character",-14+index2); }else { d.moveStart("character",index2); d.moveEnd("character",-15+index2)