6

I'm trying to compare two datepicker dates and see if they are more than 7 days apart.

How would I do this?

I would normally just see if their difference is greater than 7, but that won't account for months and such.

Here is my code:

var datepickerBegin = $("#datepicker_start").val();
var datepickerEnd = $("#datepicker_to").val();

if (datepickerBegin - datepickerEnd > 7) { 
    alert('more than a week apart!') 
}

Any tips??

streetlight
  • 5,968
  • 13
  • 62
  • 101
  • this seems to be the same question more or less - http://stackoverflow.com/questions/73971/using-javascript-how-do-i-make-sure-a-date-range-is-valid – Pyro979 Oct 10 '12 at 13:56
  • 1
    When I try to utilize his solution, the 'difference' variable is NaN. Am I lost on something? I just want to see if it is indeed seven days apart – streetlight Oct 10 '12 at 14:00

6 Answers6

18

Use $("#datepicker_xxx").datepicker("getDate") to get the picked date as a Date. Then it's just a matter of

end - begin > 7 * 86400 * 1000
Gustav Barkefors
  • 5,016
  • 26
  • 30
2

// Check the date range, 86400000 is the number of milliseconds in one day

var difference = (datepickerEnd- datepickerBegin ) / (86400000 * 7);
if (difference < 0) {
  alert("The start date must come before the end date.");
  return false;
}

if (difference <= 1) {
  alert("The range must be at least seven days apart.");
  return false;
}

return true;
Ben Thomas
  • 3,180
  • 2
  • 20
  • 38
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
2

Try this, DatePicker has a handy formatDate function which i've used to compare mm/dd/yy dates:

$.datepicker.formatDate("dd/mm/yy",new Date("09/01/2014")) < $.datepicker.formatDate("dd/mm/yy", new Date("10/01/2014")); // Returns true
$.datepicker.formatDate("dd/mm/yy",new Date("10/01/2014")) < $.datepicker.formatDate("dd/mm/yy", new Date("10/01/2014")); // Returns false
$.datepicker.formatDate("dd/mm/yy",new Date("11/01/2014")) < $.datepicker.formatDate("dd/mm/yy", new Date("10/01/2014")); // Returns false
Alex
  • 21
  • 1
1

Try this:

var datepickerBegin = $("#datepicker_start").val(); // lets, returning in mm/dd/yy format
var datepickerEnd = $("#datepicker_to").val(); // lets, returning in mm/dd/yy format

if ( ($.datepicker.parseDate('mm/dd/yy', datepickerBegin) -  $.datepicker.parseDate('mm/dd/yy', datepickerEnd)) > 7) {
alert('more than a week apart!') ;
}

Hope it should work. Thanks. For more check it out.

rony36
  • 3,277
  • 1
  • 30
  • 42
1
$(document).ready(function(){
    $("#txtFromDate").datepicker({
        minDate: 0,
        maxDate: "+60D",
        numberOfMonths: 2,
        onSelect: function(selected) {
          $("#txtToDate").datepicker("option","minDate", selected)
        }
    });
    $("#txtToDate").datepicker({ 
        minDate: 0,
        maxDate:"+60D",
        numberOfMonths: 2,
        onSelect: function(selected) {
           $("#txtFromDate").datepicker("option","maxDate", selected)
        }
    });  
});

From: <input type="text" id="txtFromDate" />

To: <input type="text" id="txtToDate" />

victorkt
  • 13,992
  • 9
  • 52
  • 51
Jasar Orion
  • 119
  • 8
  • Try to post your answer as code by indenting it with at least 4 spaces. It will be better readable then. – Rias Apr 21 '15 at 21:48
0

this is working perfectly

function checkDateDifference(startDate, endDate) {
    startDate = $.datepicker.parseDate('mm/dd/yy', startDate);
    endDate = $.datepicker.parseDate('mm/dd/yy', endDate);

    var difference = (endDate - startDate) / (86400000);
    alert(difference)
    if (difference < 0) {
        showError("The start date must come before the end date.");
        return false;
    }
    return true;

}
Ankush Jain
  • 1,532
  • 1
  • 15
  • 24