scrit
scrit

Reputation: 241

date Range selection

i was trying to get the number of days between fromDate and toDate as user should not select more than 90 days. I am using dojo to get the date calendar. Below is the code i tried but the output was not as expected, please suggest.

<script>
function parseDate(str) {
    var mdy = str.split('-')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return (second-first)/(1000*60*60*24);
}

function daysDifference(){
    var fromDate = document.getElementById("fromDate").value;
    var toDate = document.getElementById("toDate").value;
    alert(fromDate);
alert("days difference" + daydiff(parseDate(fromDate),parseDate(toDate)));
}
</script>
<form id="myform" name="myform" action="checkData.htm">


    From Date:<input type="text" name="fromDate" id="fromDate" value="" data-dojo-type="dijit/form/DateTextBox" required="true" constraints="{ datePattern: 'dd-MM-yyyy'}" /> </td>
    To Date:<input type="text" name="toDate" id="toDate" value="" data-dojo-type="dijit/form/DateTextBox" required="true" constraints="{ datePattern: 'dd-MM-yyyy'}"/> </td>
    <input type="submit" value="submit" onclick="daysDifference();"/>

</form>

Please suggest how can i restrict user to select the date range between 90days.

Upvotes: 0

Views: 262

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172548

You may try like this:

var fromDate = new Date("7/11/2014");
var toDate = new Date("12/12/2015");
var timeDiff = Math.abs(toDate.getTime() - fromDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
if(diffDays > '90')
alert("Select a date range in 90 dates from toDate");

JSFIDDLE DEMO

Upvotes: 2

Grice
Grice

Reputation: 1375

The Date.parse() method returns the number of returns the number of milliseconds between January 1, 1970. You could run parse on each date and then get the difference of the values through subtraction. You would then need to convert the time in milliseconds to a days.

Upvotes: 0

Related Questions