2016-06-07 41 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

相關問題