如何使用jQuery比较两个日期选择器日期 [英] How to compare two datepicker dates using jQuery

查看:22
本文介绍了如何使用jQuery比较两个日期选择器日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

var $from = $("#fromDate").datepicker('getDate');
var $to = $("#toDate").datepicker('getDate');
if($from > $to)
   alert("from date shouldn't greater than To date");

如果它是同一年的两个日期,它就可以工作.否则,例如fromDate='1/12/2012'(dd/mm/yyyy) toDate='18/6/2013'(dd/mm/yyyy),当您检查条件时,它不起作用.它抛出一个警报,这是给定的.

It is working if it is two dates in the same year. Otherwise, for example fromDate='1/12/2012'(dd/mm/yyyy) toDate='18/6/2013'(dd/mm/yyyy), while you check the condition, it is not working. It throws an alert, which is given.

推荐答案

改为自动限制日期.在此示例中,第二个组合框不允许您选择比您在第一个组合框中选择的日期低的日期.

Auto limit your dates instead. In this example the second combo box won't allow you to pick a lower date than the one you pick on the first one.

$(document).ready(function() {

  $("#txtFromDate").datepicker({

    numberOfMonths: 2,

    onSelect: function(selected) {
      $("#txtToDate").datepicker("option", "minDate", selected)
    }
  });

  $("#txtToDate").datepicker({

      numberOfMonths: 2,

      onSelect: function(selected) {
         $("#txtFromDate").datepicker("option", "maxDate", selected)

      }
  });
});

这是工作演示.

这篇关于如何使用jQuery比较两个日期选择器日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆