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’);
})