JavaScript中日期之间的比较

时间:2014-09-30 06:53:45

标签: 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;    
}

但我没有收到输出错误。非常感谢。

1 个答案:

答案 0 :(得分: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);
    });