jQuery 如何比较两个datepicker日期jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17169772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to compare two datepicker date jquery
提问by Kanagaraj Vadivel
anyone could you help me guys. this is my code
任何人都可以帮助我,伙计们。这是我的代码
var $from=$("#fromDate").datepicker('getDate');
var $to =$("#toDate").datepicker('getDate');
if($from>$to)
alert("from date shouldn't greater than To date");
it is working if it is two date same year year. otherwise for example fromDate='1/12/2012'(dd/mm/yyyy) toDate='18/6/2013'(dd/mm/yyyy), while you check condition, it is not working. it throws alert, which i given.
如果它是同一年的两个日期,它就可以工作。否则例如 fromDate='1/12/2012'(dd/mm/yyyy) toDate='18/6/2013'(dd/mm/yyyy),当您检查条件时,它不起作用。它引发警报,这是我给出的。
回答by isJustMe
Can I suggest auto limiting your dates instead? In this example the 2nd combo box won't allow you to pick a lower date than the one you pick on the fisrt 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)
}
});
});
Here is a working demo
这是一个工作演示
回答by Setup
You need to use this to get the day/month/year
您需要使用它来获取日/月/年
var day1 = $("#datepicker").datepicker('getDate').getDate();
var month1 = $("#datepicker").datepicker('getDate').getMonth() + 1;
var year1 = $("#datepicker").datepicker('getDate').getFullYear();
After that you can compare the values.
之后,您可以比较这些值。
回答by Asif Raza
Date.parse(fromDate) > Date.parse(toDate)
Example
例子
var sDate = $('#EFT_FRM_DATE').val();
var eDate = $('#EFF_TO_DATE').val();
if (Date.parse(sDate) > Date.parse(eDate) || Date.parse(sDate) == Date.parse(eDate)) {
ShowMessage(CurrencySetupExchangeIndex.EndDateGreaterStartDate, 'Error');
//alert
return false;
return;
}
simplified statement:
简化语句:
if (Date.parse(sDate) >= Date.parse(eDate)) {...
回答by vishwajeet samddar
$(document).ready(function(){
$("#startdate").datepicker({
todayBtn: 1,
format: "dd-mm-yyyy",
startDate: '1d',
autoclose: true,
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
$('#enddate').datepicker('setStartDate', minDate);
if($('#enddate').val() !="" && ($("#enddate").datepicker("getDate") == null || Date.parse($("#enddate").datepicker("getDate"))< Date.parse($("#startdate").datepicker("getDate")))){
var date = new Date($("#startdate").datepicker("getDate")).toLocaleDateString();
date = date.split("/")
date = date[0]+"-"+date[1]+"-"+date[2]
$('#enddate').val(date)
}
});
$("#enddate").datepicker({
format: "dd-mm-yyyy",
autoclose: true,
})
});