man_or_astroman
man_or_astroman

Reputation: 648

jquery difference between given dates

Im trying to calculate the days between two dates but i dont want just the days difference, what i what is to write a script in order to show me all the actual dates between this 2 given dates.. For example i have 2 dates:

3/12/2013 and 3/15/2013

i want somehow to get these dates: 3/12/2013, 3/13/2013, 3/14/2013, 3/15/2013

Here is the code ive wrote but this only return the difference in days

$(document).ready(function () {

  var From = "03-12-2013";
  var To = "03-15-2013";
  var d1 = jQuery.datepicker.parseDate("mm-dd-yy",  From);
  var d2 = jQuery.datepicker.parseDate("mm-dd-yy",  To);

  var diff = 0;
  if (d1 && d2) {
      diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
  }
  console.log(diff);
});

thank you

Upvotes: 2

Views: 585

Answers (1)

Christoph
Christoph

Reputation: 51211

Without looking at datepicker (perhaps there is a method already giving you what you need) based on your code you could just do something like this:

var milisperday = 86400000,
    days = [],
    offset = (d1<d2)?d1:d2;

for (var i = 1;i<=diff;i++){
    days.push(new Date(offset.getTime() + milisperday * i));
}

console.log(days);

now days is an array containing your desired days as Date Objects.

Example

Upvotes: 1

Related Questions