I have 2 asp.net texboxes with calendar extender. I want to find out the number of days between both dates when one of the date control is changed. how can I achieve this using jquery or javascript?
Asked
Active
Viewed 1.8e+01k times
51
-
3Maybe this help you http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-jquery – Keyne Viana Apr 09 '10 at 17:27
-
@Aneef, you might want to update the title of your question. It says "checkboxes" where I'm guessing you meant to say "textboxes". – maček Apr 09 '10 at 17:40
9 Answers
98
This should do the trick
var start = $('#start_date').val();
var end = $('#end_date').val();
// end - start returns difference in milliseconds
var diff = new Date(end - start);
// get days
var days = diff/1000/60/60/24;
Example
var start = new Date("2010-04-01"),
end = new Date(),
diff = new Date(end - start),
days = diff/1000/60/60/24;
days; //=> 8.525845775462964

maček
- 76,434
- 37
- 167
- 198
-
5For me, var diff = new Date(Date.parse(to) - Date.parse(from)) works. Great answer nevertheless :) – Gogol Mar 10 '14 at 11:10
-
1`(Date.parse("2019-01-02") - Date.parse("2019-01-01")) / 86400000` // => 1 day – beginner Apr 24 '19 at 11:35
11
1) Html
<input type="text" id="firstDate" name="firstDate"/>
<input type="text" id="secondDate" name="secondDate"/>
2) Jquery
$("#firstDate").datepicker({
});
$("#secondDate").datepicker({
onSelect: function () {
myfunc();
}
});
function myfunc(){
var start= $("#firstDate").datepicker("getDate");
var end= $("#secondDate").datepicker("getDate");
days = (end- start) / (1000 * 60 * 60 * 24);
alert(Math.round(days));
}
Jsfiddle working example here

Shehary
- 9,926
- 10
- 42
- 71

Aravindh Gopi
- 2,083
- 28
- 36
4
Hi, This is my example of calculating the difference between two dates
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://code.jquery.com/jquery.min.js"></script>
<title>JS Bin</title>
</head>
<body>
<br>
<input class='fromdate' type="date" />
<input class='todate' type="date" />
<div class='calculated' /><br>
<div class='minim' />
<input class='todate' type="submit" onclick='showDays()' />
</body>
</html>
This is the function that calculates the difference :
function showDays(){
var start = $('.fromdate').val();
var end = $('.todate').val();
var startDay = new Date(start);
var endDay = new Date(end);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = endDay.getTime() - startDay.getTime();
var days = millisBetween / millisecondsPerDay;
// Round down.
alert( Math.floor(days));
}
I hope I have helped you

Said Erraoudy
- 1,490
- 1
- 13
- 21
2
Number of days calculation between two dates.
$(document).ready(function () {
$('.submit').on('click', function () {
var startDate = $('.start-date').val();
var endDate = $('.end-date').val();
var start = new Date(startDate);
var end = new Date(endDate);
var diffDate = (end - start) / (1000 * 60 * 60 * 24);
var days = Math.round(diffDate);
});
});

Ravi Shankar
- 31
- 2
0
This is how I did it using the Math.floor() function:
var start = new Date($('#start').val());
var end = new Date($('#end').val());
var diff = Math.floor((end-start) / (365.25 * 24 * 60 * 60 * 1000));
console.log(diff);
You could also do it this way using the Math.round() function:
var start = new Date($('#start').val());
var end = new Date($('#end').val());
var diff = new Date(end - start) / (1000 * 60 * 60 * 24 * 365.25);
var age = Math.round(diff);
console.log(age);

sylenix
- 161
- 2
- 8
0
**This is a simple way of getting the DAYS between two dates**
var d1 = moment($("#StartDate").data("DateTimePicker").date());
var d2 = moment($("#EndDate").data("DateTimePicker").date());
var diffInDays = d2.diff(d1, 'days');
if (diffInDays > 0)
{
$("#Total").val(diffInDays);
}
else
{
$("#Total").val(0);
}

Adnan Cindioglu
- 33
- 1
- 6
0
var days=0;
function myfunc(){
var start= $("#firstDate").datepicker("getDate");
var end= $("#secondDate").datepicker("getDate");
days = (end- start) / (1000 * 60 * 60 * 24);
alert(Math.round(days));
}

Techy
- 2,626
- 7
- 41
- 88

Pergin Sheni
- 393
- 2
- 11
-
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – β.εηοιτ.βε Jun 12 '20 at 18:31
0
$("#edate").change(function(){
var edate = new Date($('#edate').val());
var sdate = new Date($('#sdate').val());
days = (edate- sdate) / (1000 * 60 * 60 * 24);
days=days+1;
alert (days);
$("#nod").val(days);
});

Corné
- 1,304
- 3
- 13
- 32
-3
This is a simple way of getting the DAYS between two dates
var nites = dateDiff(arriveDate,departDate,"D");

Ravi Ram
- 24,078
- 21
- 82
- 113