JSFiddle - React, Tailwind, and code Playground
by Sascha
HTML
<div id="results">
</div>
JavaScript
/**
* Calculate time spans utility.
*/
TimeSpanUtil= new function() {
var self= this;
/**
* The function returns a duration calculated on the provided values.
* In case border values have been provided, these are respected in case a "from" or "until" value is lower or higher
* than the provided values.
* @param from
* @param until
* @param lowerTimeBorder
* @param upperTimeBorder
* @returns time in milliseconds
*/
self.calculateTimeSpan= function(from, until, lowerTimeBorder, upperTimeBorder) {
if (typeof(lowerTimeBorder) !== 'undefined' && typeof(upperTimeBorder) !== 'undefined') {
if (lowerTimeBorder > from) {
from= lowerTimeBorder;
}
if (upperTimeBorder < until) {
until= upperTimeBorder;
}
}
return new Date(until) - new Date(from);
}
};
document.getElementById("results").append(
TimeSpanUtil.calculateTimeSpan('2018-01-01 00:00:00', '2018-02-01 00:00:00') + '<br />'
);
document.getElementById("results").append(
TimeSpanUtil.calculateTimeSpan('2018-01-01 00:00:00', '2018-02-01 00:00:00',
'2018-01-10 00:00:00', '2018-01-11 00:00:00'
) + '<br />'
);