2014-09-30 123 views
0

我有兩個日期字段在文本字段中表示,代碼如下所示。JavaScript中的日期之間的比較

$(function() 
{ 
$("#Start_Date").datepicker({ dateFormat: 'dd-M-yy', 
    showOn: "button", 
    buttonImage: "../I/dateimg.png", 

    buttonImageOnly: true, 
    firstDay:1, 
    minDate: -0, 
    beforeShowDay: checkDisabled, 
    changeMonth: true, 
    changeYear: true, 
    beforeShow: function(input, inst) 
    { inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth-210 + 'px'}); }}); 


$("#End_Date").datepicker({ dateFormat: 'dd-M-yy', 
    showOn: "button", 
    buttonImage: "../I/dateimg.png", 

    buttonImageOnly: true, 
    firstDay:1, 
    minDate: -0, 
    beforeShowDay: checkDisabled, 
    changeMonth: true, 
    changeYear: true, 
    beforeShow: function(input, inst) 
    { inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth-210 + 'px'}); }}); 
    }); 

我也有一個函數用來比較日期,並返回true,只要開始日期小於結束日期。該功能如下所示。

function CompairLeaveDate() 
{ 
var BlockStartDate = $('#Start_Date').val(); 
var ExplodeStartDate=BlockStartDate.split('-'); 
var dt1 = ExplodeStartDate[0]; 
var mon1 = ExplodeStartDate[1]; 
var yr1 = ExplodeStartDate[2]; 

var totalLength=js_MonthArray.length; 
var i=0; 

var BlockEndDate = $('#End_Date').val(); 
var ExplodeEndDate=BlockEndDate.split('-'); 
var dt2 = ExplodeEndDate[0]; 
var mon2 = ExplodeEndDate[1]; 
var yr2 = ExplodeEndDate[2]; 
var y=document.getElementById("End_Date"); 

if(dt2 < dt1) 
{ 
    document.getElementById('End_Date').focus(); 
    y.style.border = '1px solid #ff6000'; 
    document.getElementById("div_End_DateError").innerHTML=getErrorMsg(1100986); 
    document.getElementById("div_End_DateError").style.display = ""; 
    return false; 
} 
else 
{ 
    y.style.border = ''; 
    document.getElementById("div_End_DateError").innerHTML =''; 
    document.getElementById("div_End_DateError").style.display = "none";  
    return true; 
} 
return true;  
} 

但我沒有得到任何輸出錯誤。任何幫助表示讚賞。

+3

這是JavaScript,而不是PHP。 – 2014-09-30 06:54:30

回答

0
$(function() { 
    var pickerOpts = { 
      dateFormat: 'dd-M-yy', 
      showOn: "button", 
      buttonImage: "../I/dateimg.png", 

      buttonImageOnly: true, 
      firstDay:1, 
      minDate: -0, 
      beforeShowDay: checkDisabled, 
      changeMonth: true, 
      changeYear: true, 
      onSelect: function(selected) { 
      $("#End_Date").datepicker("option","minDate", selected) 
     } 
     }; 
$("#Start_Date").datepicker(pickerOpts); 
}); 



$(function() { 
     var pickerOpts = { 
       dateFormat: 'dd-M-yy', 
       showOn: "button", 
       buttonImage: "../I/dateimg.png", 

       buttonImageOnly: true, 
       firstDay:1, 
       minDate: -0, 
       beforeShowDay: checkDisabled, 
       changeMonth: true, 
       changeYear: true, 
      onSelect: function(selected) { 
      $("#Start_Date").datepicker("option","maxDate", selected) 
      } 
      }; 
    $("#End_Date").datepicker(pickerOpts); 
    });