2012-03-07 178 views
3

我使用Calender插件分別從&到兩個日期選擇。我需要比較value的值,並始終大於使用jQuery的值,只要我選擇日期。使用jQuery比較兩個日期

我使用下面的代碼

var fromDate = $("#from").val(); 
var toDate = $("#to").val(); 
if (Date.parse(fromDate) > Date.parse(toDate)) { 
    alert("Invalid Date Range!\nStart Date cannot be after End Date!") 
    return false; 
} 

和HTML代碼是:

<input type="text" name="from" id="from" value="" class="datepicker validate[custom[date]]" tabindex="4" /> 
<input type="text" name="from" id="from" value="" class="datepicker validate[custom[date]]" tabindex="4" /> 

如果我用比較jQuery驗證插件:

<input value="" class="validate[required,custom[date],future[2009-01-01]" type="text" id="d1" name="d1" /> 

正如http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

+1

檢查現有的副本。 http://stackoverflow.com/questions/658522/age-from-date-of-birth-using-jquery – Teja 2012-03-07 05:15:16

+0

我用我的情況下,純JS它 – neoascetic 2012-03-07 05:15:23

+0

IF((新的日期(FROM日期).getTime ()> new Date(toDate).getTime())) – Ghostman 2012-03-07 05:16:57

回答

4

你必須改變你的HTML代碼,你已經使用相同的ID和名稱的兩個領域。根據以下內容更改

<input type="text" name="from" id="from" value="" class="datepicker validate[custom[date]]" tabindex="4" /> 
<input type="text" name="to" id="to" value="" class="datepicker validate[custom[date]]" tabindex="4" /> 
+2

請檢查此鏈接http://jqueryui.com/demos/datepicker/#date-range可能對您有所幫助 – pkachhia 2012-03-07 05:22:08

+1

+1。這裏的工作示例 - http://jsfiddle.net/LBSAp/ – Phil 2012-03-07 05:22:37

+0

即使沒有幫助 – Ankit 2012-03-07 05:23:36