2016-06-07 63 views
0

我有一个时间/日期转换器。例如,当用户输入“130”时,例如当他们输入“6 7 16”时返回“06/07/2016 01:30:00”,例如返回“06/07/2016 00:00:00”,但当用户输入“6 7 16 130”为例,它返回“2016年6月7日假:00” 这里是我的相关代码(可以显示更多的如果需要的话):最后一个数组元素不断返回false

function checkDateTime(val) { 
    var nowDate = new Date(); 

    var month, day, year, time; 
    var ar;  
    if (eval(val)) { 
     var tval = val.value; 
     ar = tval.split(' '); 
     if (ar.length === 3) { // i.e. if it's supposed to be a date 
    ar[0] = month; 
    ar[1] = day; 
    ar[2] = year; 
    document.getElementById("FromDate").value = CheckDate(val) + ' ' + '00:00:00'; 
    //checkDate(ar[0] + ' ' + ar[1] + ' ' + ar[2]); 
     } 

    //alert(LeftPadZero(ar[0]) + ' ' + LeftPadZero(ar[1]) + ' ' + LeftPadZero(ar[2])); 
    //alert(CheckDate(ar[0] + ' ' + ar[1] + ' ' + ar[2])); 

     if (ar.length === 1) { // if it's a time 
    ar[0] = time; 
    var MM = nowDate.getMonth() + 1; 
    var DD = nowDate.getDate(); 
    var Y = nowDate.getFullYear(); 
    var nowDateFormat = LeftPadZero(MM) + '/' + LeftPadZero(DD) + '/' + Y; 
    alert(ar[0]); 
    document.getElementById("FromDate").value = nowDateFormat + ' ' + checktime(val) + ':00'; 
     } 

    if (ar.length === 4) { // if date and time 

     ar[0] = month; 
    // alert(ar[0]); 
     ar[1] = day; 
    // alert(ar[1]); 
     ar[2] = year; 
    // alert(ar[2]); 
     ar[3] = time; 
    // alert(ar[3]); 

     document.getElementById("FromDate").value = CheckDate(val) + ' ' + checktime(val) + ':00'; 

    // alert(ar[0] + ' ' + ar[1] + ' ' + ar[2] + ' ' + ar[3]); 
    } 
    } 
} 

function CheckDate(theobj) { 
    var isInvalid = 0; 
    var themonth, theday, theyear; 
    var arr; 
    if (eval(theobj)) { 
     var thevalue = theobj.value; 
     arr = thevalue.split(" "); 
     if (arr.length < 2) { 
      arr = thevalue.split("/"); 
      if (arr.length < 2) { 
       arr = thevalue.split("-"); 
       if (arr.length < 2) { 
        isInvalid = 1; 
       } 
      } 
     } 
     if (isInvalid == 0) { 
      themonth = arr[0]; 
      theday = arr[1]; 
      if (arr.length == 3) { 
       theyear = arr[2]; 
      } else { 
       theyear = new Date().getFullYear(); 
      } 
      if (isNaN(themonth)) { 
       themonth = themonth.toUpperCase(); 
       //month name abbreviation array 
       var montharr = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]; 
       for (i = 0; i < montharr.length; i++) { 
        //if the first 3 characters of month name matches 
        if (themonth.substring(0, 3) == montharr[i]) { 
         themonth = i + 1; 
         break; 
        } 
       } 
      } else { 
       if (themonth < 1 || themonth > 12) { 
        isInvalid = 1; 
       } 
      } 
     } 




     if (isNaN(themonth) || isNaN(theday) || isNaN(theyear)) { 
      isInvalid = 1; 
     } 

     if (isInvalid == 0) { 
      var thedate = LeftPadZero(themonth) + "/" + LeftPadZero(theday) + "/" + LeftPadZero(theyear); 
      return thedate; 
     } else { 
      return false; 
    } 
    } 
} 

function checktime(x) { 
    var tempchar = new String; 
    tempchar = MakeNum(x); 
    if (tempchar != '' && tempchar.length < 4) { 
     //e.g., if they enter '030' make it '0030' 
     if (tempchar.length == 3) { 
      tempchar='0' + tempchar; 
     } 
     //e.g, if they enter '11' make it '1100' 
     if (tempchar.length == 2) { 
      tempchar=tempchar + '00'; 
     } 
     //e.g, if they enter '6' make it '0600' 
     if (tempchar.length == 1) { 
      tempchar='0' + tempchar + '00'; 
     } 
     } 
     if (tempchar==null || tempchar == '') { 
     return false; 
     } 
     else { 
      if (tempchar=='2400') { 
      return false; 
      }else{ 
      var tempnum= new Number(tempchar); 
      var swmin = new Number(tempnum % 100); 
      var swhour = new Number((tempnum-swmin)/100); 
      if (swhour < 25 && swmin < 60) { 
       x = LeftPadZero(swhour) + ":" + LeftPadZero(swmin); 
     return x; 
      } 
      else { 
       return false; 
      } 
      } 
    } 
return false; 

/* 
    if(eval(changecount)!=null){ 
     changecount+=1; 
    } 
*/ 
} 

function MakeNum(x) { 
    var tstring = new String(x.value); 
    var tempchar = new String; 
    var f = 0; 
    for (var i = 0; i < tstring.length; i++) { 
      // walk through the string and remove all non-digits 
     chr = tstring.charAt(i); 
     if (isNaN(chr)) { 
      f=f; 
      } 
     else { 
      tempchar += chr; 
      f++; 
      } 
     }   
     return tempchar; 
} 

我已经试过无数的事情找出时间元素为什么在长度为4的数组中返回false,但由于某种原因而不是数组长度1,包括设置各种警报并检查控制台。我多次搜索了这个问题,然后空着。

重申,我的问题是时间元素在4的数组中返回false,我试图完成的是用户输入日期和时间,并使它们都格式化并正确显示。

任何人都可以提供帮助和/或提供建议和/或建议吗?谢谢!

编辑:用户输入'130'应该转换为'06/07/2016(今天的日期)01:30:00' 6 7 16应该转换为'06/07/2016 00:00:00' 6 7 16 130应该转换为'06/07/2016 01:30:00'

+0

'的eval()'?那永远不是一个好兆头...... –

+0

你是什么意思? – Berz

+0

他只是说使用eval不当会打开注入攻击代码等等。 –

回答

0

这里似乎有一些缺失的部分...各种功能和这些需要的任何输入类型都不包括在您的文章中。 。但是,我猜想,当你正在做最后的“checktime”调用时,而不是传递完整的“val”变量时,你应该只是传递最后一块分割输入“ar [3]“在这种情况下。这样,只有那件作品被评估。

IE:

document.getElementById("FromDate").value = CheckDate(val) + ' ' + checktime(val) + ':00'; 

应该

document.getElementById("FromDate").value = CheckDate(val) + ' ' + checktime(ar[3]) + ':00'; 

同样,这只是一个猜测,由于缺少的部分。

编辑: 在得到一些额外的细节之后,问题似乎在发送到checktime函数的数据中,但是,由于当前的代码设置,修复实际上只是确保正在处理的数据通过检查时间功能是数组中唯一的最后一个项目...参见下面的检查时间函数中的修正:

tempchar = MakeNum(x); 

成为

tempchar = MakeNum(x).split(' ').pop(); 
+0

试过。它会抛出错误“can not read property'value of'undefined”for this line:var tstring = new String(x.value);在MakeNum(x)函数中。我试图只显示相关的代码,但如果需要的话可以显示更多 – Berz

+0

那么这些函数预期哪种类型的数据?什么是“x”预计会是? –

+0

该函数给了我。 checkDateTime函数是我唯一的。我曾假设x的意思是什么变量函数在 – Berz

相关问题