当前位置: 代码迷 >> JavaScript >> 【转】JS 日期与日期时间 自动扩住亟需的字段
  详细解决方案

【转】JS 日期与日期时间 自动扩住亟需的字段

热度:153   发布时间:2012-10-21 09:00:08.0
【转】JS 日期与日期时间 自动扩住需要的字段
/**
 * 日期的控件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);
 }
 d.select();
}

/**
 * YYYY-MM-DD HH:MM格式是否正确,传入参数为text的value
*/
function isTimeFormatOK(DataValue) {//时间格式是否正确的判断
 var returnTarget = false;
 
 if(DataValue != null && DataValue.length == 16){
  
  var year = DataValue.substring(0,4);
  var month = DataValue.substring(5,7);
  var day = DataValue.substring(8,10);
  var hour = DataValue.substring(11,13);
  var min = DataValue.substring(14,16);
  
  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;
     }
    }
   }
  }
  if(parseInt(hour,10)<0 || parseInt(hour,10)>24){
   returnTarget = false;
  }
  if(parseInt(min,10)<0 || parseInt(min,10)>59){
   returnTarget = false;
  }
 }
 return returnTarget;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 输入的值中,是否存在M/Y/D/H
*/
function checkYMDH(value){
       var   regu1   =   "M"  ;    
    var   re1   =   new   RegExp(regu1);  
    
    var   regu2   =   "Y"  ;    
    var   re2   =   new   RegExp(regu2);
    
    var   regu3   =   "D"  ;    
    var   re3   =   new   RegExp(regu3);
    
    var   regu4   =   "H"  ;    
    var   re4   =   new   RegExp(regu4);
    
     if(value.search(re1)   !=   -1 || value.search(re2)   !=   -1 || value.search(re3)   !=   -1 || value.search(re4)   !=   -1 )       
     {      
         return true;    
     }      
     else      
     {    
         return false; 
      } 
 
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 初始化时间控件
*/
function initTimeTxt(TableLotID){
 var txts = document.getElementById(TableLotID).getElementsByTagName("input");  
 for(var i=0;i<txts.length;i++)  
 {  
 if(txts[i].type=="text" && txts[i].name == "DATATXT"){  
 initTimePage(txts[i].id);
 }  
 if(txts[i].type=="text" && txts[i].name == "DATATIMETXT"){  
 initTime2Page(txts[i].id);
 } 
 }
}

?

  相关解决方案