Moment.js - two dates difference in number of days
From the moment.js docs: format('E')
stands for day of week. thus your diff is being computed on which day of the week, which has to be between 1 and 7.
From the moment.js docs again, here is what they suggest:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
Here is a JSFiddle for your particular case:
$('#test').click(function() {
var startDate = moment("13.04.2016", "DD.MM.YYYY");
var endDate = moment("28.04.2016", "DD.MM.YYYY");
var result = 'Diff: ' + endDate.diff(startDate, 'days');
$('#result').html(result);
});
#test {
width: 100px;
height: 100px;
background: #ffb;
padding: 10px;
border: 2px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>
<div id='test'>Click Me!!!</div>
<div id='result'></div>
Here's how you can get the comprehensive full fledge difference of two dates.
function diffYMDHMS(date1, date2) {
let years = date1.diff(date2, 'year');
date2.add(years, 'years');
let months = date1.diff(date2, 'months');
date2.add(months, 'months');
let days = date1.diff(date2, 'days');
date2.add(days, 'days');
let hours = date1.diff(date2, 'hours');
date2.add(hours, 'hours');
let minutes = date1.diff(date2, 'minutes');
date2.add(minutes, 'minutes');
let seconds = date1.diff(date2, 'seconds');
console.log(years + ' years ' + months + ' months ' + days + ' days ' + hours + '
hours ' + minutes + ' minutes ' + seconds + ' seconds');
return { years, months, days, hours, minutes, seconds};
}
Use moment.js
to find the difference between two dates in days.
Install moment.js using the following command,
npm install moment
Calculate the difference between two dates in days.
var moment = require('moment');
var date1 = moment("2022-10-30");
var date2 = moment("2022-12-30");
var days = date1.diff(date2, 'days')