最后一个数组元素保持返回false

时间:2016-06-07 19:46:37

标签: javascript html arrays html5

我有时间/日期转换器。当用户输入" 130"例如它返回" 06/07/2016 01:30:00"当他们进入" 6 7 16"例如它返回" 06/07/2016 00:00:00"但是当用户输入" 6 7 16 130"例如,它返回" 06/07/2016 false:00" 这是我的相关代码(如果需要可以显示更多):

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;
}

我已经尝试了很多东西来弄清楚为什么time元素在长度为4的数组中返回false,但由于某种原因没有数组长度为1,包括设置各种警报和检查控制台。我已经多次搜索过这个问题并且空了。

重申一下,我的问题是time元素在4的数组中返回false,而我想要完成的是让用户输入日期和时间,并将它们格式化并正确显示。

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

编辑:用户输入&#39; 130&#39;应转换为2016年6月6日(今天及之前的日期)01:30:00&#39; 6 7 16应转换为&00; 06/07/2016 00:00:00&#39; 6 7 16 130应转换为&#39; 06/07/2016 01:30:00&#39;

1 个答案:

答案 0 :(得分:0)

这里似乎有一些缺失的部分...各种功能以及这些所需的输入类型都被排除在你的帖子之外......但是,猜猜看,我会在你制作时说出来你的最终&#34;检查时间&#34;打电话,而不是传递完整的&#34; val&#34;变量,你应该只是传递你的分割输入的最后一个块,&#34; ar [3]&#34;在这种情况下。这样,只有该部分由该函数评估。

IE:

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

应该是

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

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

编辑: 在获得一些额外的细节后,问题似乎在于发送到checktime函数的数据,但是,由于当前的代码设置,修复实际上只是确保checktime函数处理的数据只是数组中的最后一项...请参阅下面的checktime函数中的更正:

tempchar = MakeNum(x);

变为

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