Difference between two dates in years, months, days in JavaScript

Hi Members,

I am new to this, I am looking for javascript to get How many years month and days to get automatically in the textbox based upon two dates entered. Please see below for my code and correct me where i did wrong

function calculate() {
var fromDate = document.getElementById(‘StartDate’).value;
var toDate = document.getElementById(‘LeavingDate’).value;

try {
document.getElementById(‘ServiceTime’).innerHTML = ‘’;

var result = getDateDifference(new Date(fromDate), new Date(toDate));

if (result && !isNaN(result.years)) {
  document.getElementById('ServiceTime').innerHTML =
    result.years + ' year' + (result.years == 1 ? ' ' : 's ') +
    result.months + ' month' + (result.months == 1 ? ' ' : 's ') + 'and ' +
    result.days + ' day' + (result.days == 1 ? '' : 's');
}

} catch (e) {
console.error(e);
}
}

function getDateDifference(startDate, endDate) {
if (startDate > endDate) {
console.error(‘Start date must be before end date’);
return null;
}
var startYear = startDate.getFullYear();
var startMonth = startDate.getMonth();
var startDay = startDate.getDate();

var endYear = endDate.getFullYear();
var endMonth = endDate.getMonth();
var endDay = endDate.getDate();

// We calculate February based on end year as it might be a leep year which might influence the number of days.
var february = (endYear % 4 == 0 && endYear % 100 != 0) || endYear % 400 == 0 ? 29 : 28;
var daysOfMonth = [31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var startDateNotPassedInEndYear = (endMonth < startMonth) || endMonth == startMonth && endDay < startDay;
var years = endYear - startYear - (startDateNotPassedInEndYear ? 1 : 0);

var months = (12 + endMonth - startMonth - (endDay < startDay ? 1 : 0)) % 12;

// (12 + …) % 12 makes sure index is always between 0 and 11
var days = startDay <= endDay ? endDay - startDay : daysOfMonth[(12 + endMonth - 1) % 12] - startDay + endDay;

return {
years: years,
months: months,
days: days
};
}

fd.rendered(function() {
function BCO() {
if (fd.field(‘LeavingDate’).value!= null) {
calculate();

    } }
	// Calling  when the user changes the Start Date
fd.field('LeavingDate').$on('change',BCO);

// Calling  on form loading
BCO();
calculate();

});

Hi inahp,

There is a ready-made solution for your case. Try Moment.js:
https://momentjs.com/

To launch it on Sharepoint you should download two .js files from the site and upload them in the document library (Site Assets, for example).

Here is an example of code calculating difference between two dates:

window.$ = $;
window.define = null;

$.getScript( “/sites/third/SiteAssets/moment-with-locales.min.js”)
.then(function() { return $.getScript( “/sites/third/SiteAssets/moment-with-locales.min.js”) })
.then(function() {
var ends = moment(‘2015-03-09’);
var starts = moment(‘2014-05-10’);
var years = ends.diff(starts, ‘year’);
starts.add(years, ‘years’);
var months = ends.diff(starts, ‘months’);
starts.add(months, ‘months’);
var days = ends.diff(starts, ‘days’);
console.log(years + ’ years ’ + months + ’ months ’ + days + ’ days’);
})