-4

I have a start-date and end-date and I want to calculate array of dates between these days based on a given duration.
for example,
if start date is 01/01/2015 and end date is 01/06/2015 and if I give duration as 3 months then out put should be:

01/04/2015  
01/06/2015  

How to achieve this using JavaScript and I need to display it in a form.

soumya
  • 1
  • 1
  • 3
  • It's not clear whether you're *actually* trying to do this in Javascript or Java. Where do JSPs come in? If all the code is server-side, Javascript is a red herring. If it's all client-side, Java and JSPs are red herrings... – Jon Skeet Apr 27 '15 at 10:49
  • I am trying to do it in javascript – soumya Apr 27 '15 at 10:52
  • Soumya, please show what you have tried, and what difficulties you encountered. Just asking for others to solve a problem for you is not the in the spirit of StackOverflow. See http://stackoverflow.com/help/how-to-ask For a similar question, see http://stackoverflow.com/questions/13146418/find-all-the-days-in-a-month-with-date-object/13146828#13146828 – Ruan Mendes Apr 27 '15 at 11:23

1 Answers1

2

If you want to calculate difference between two dates using :

Then,

function dateDiff() { 
   var dtFrom = document.getElementById('txtFromDate').value;
   var dtTo = document.getElementById('txtToDate').value;

   var dt1 = new Date(dtFrom);
   var dt2 = new Date(dtTo);
   var diff = dt2.getTime() - dt1.getTime();
   var days = diff/(1000 * 60 * 60 * 24);
   alert(dt1 + ", " + dt2);

   alert(days);
   return false;
}
function isNumeric(val) {
   var ret = parseInt(val);
}

HTML:

<label for="txtFromDate">From Date : </label>
   <input type="text" id="txtFromDate" name="txtFromDate" size="10" maxlength="10" value="03/25/2013"/><br/>
   <label for="txtToDate">To Date : </label>
   <input type="text" id="txtToDate" name="txtDate" size="10" maxlength="10" value="03/26/2013"/><br/>
   <button id="btnCheck" name="btnCheck" onClick="dateDiff();" type="button">Difference</button>

AFTER EDIT:

Following solution is to get all dates between specified dates.

Working Demo

// using Datepicker value example code
$('#getBetween').on('click', function () {
    var start = $("#from").datepicker("getDate"),
        end = $("#to").datepicker("getDate");
    var between = getDates(start, end);
    $('#results').html(between.join('<br> '));
});

// This function doing this work.
function getDates(start, end) {
    var datesArray = [];
    var startDate = new Date(start);
    while (startDate <= end) {
        datesArray.push(new Date(startDate));
        startDate.setDate(startDate.getDate() + 1);
    }
    return datesArray;
}
Vikrant
  • 4,920
  • 17
  • 48
  • 72
  • I don't want to calculate difference between two dates i want array of dates between given start and end date based on some duration – soumya Apr 27 '15 at 11:03
  • @soumya, added new edit below existing answer! also added a [working demo](http://jsfiddle.net/Vikrant29/NUfyn/7/) of it. Let me know if it's helpful! – Vikrant Apr 27 '15 at 11:22
  • 1
    @Vikrant, I think you should also send him the bill for your work (obvious give me the codez post), but for your effort i'm willing to upvote you ;) – Icepickle Apr 27 '15 at 12:07
  • @Vikrant pretty close to what I was looking for, only have a query; how do I send the dates to another View through controller instead of using the
    . I wish to take the dates back to controller and then pass it to a table in another view. Lets say from View B to MainController and then to View A. Best Regards.
    –  Nov 21 '17 at 13:31