I know this question has been asked a thousand times, but im trying to get javascript to display me the days between two dates. I have seen this post:How do i get the number of days between two dates in javascript
From one of the comments I have used this code:
<input type="text" name="sod" class="startd" value="10/02/2016" />
<input type="text" name="dos" class="endd" value="12/02/2016" />
<script>
function treatAsUTC(date) {
var result = new Date(date);
result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
return result;
}
function daysBetween(startDate, endDate) {
var millisecondsPerDay = 24 * 60 * 60 * 1000;
return (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
}
alert(daysBetween($('.startd').val(), $('.endd').val()));
</script>
The reulst from the javascript give 61 days, but I want it to read as dd/mm/yyyy not mm/dd/yyyy as it currently is, so the result should be 2 days.
I have tried removing the treatAsUTC parts but it then desont give any answer at all.
function daysBetween(startDate, endDate) {
var millisecondsPerDay = 24 * 60 * 60 * 1000;
return (endDate - startDate) / millisecondsPerDay;
}
alert(daysBetween($('.startd').val(), $('.endd').val()));
can any one help or guide me in the right direction?
Thanks in advance.
Ian