My application consists of an API built using Spring Boot and a view created with AngularJS. The app retrieves a report indicating when an incident started and ended, with examples like startdate(2015-12-01T08:19:00.000Z) and enddate(2015-12-06T02:59:00.000Z).
I'm struggling to find an efficient way to calculate the time difference between these two dates and assign it to a new variable called "unavailability."
Below is my JavaScript file:
.controller(
'IncidenteController',
[
'$scope',
'$http',
'$routeParams',
'urlinc',
'$location',
function($scope, $http, $routeParams, urlinc, $location) {
var url = urlinc.getUrlinc();
var onError = function(reason) {
$scope.error = "Could not retrieve data";
};
var code = $routeParams.codename;
console.log(code);
// Various functions for retrieving data from different endpoints
$scope.saveIncidente = function(incidente) {
// save incident logic
};
// Other functions and event listeners
var that = this;
// Object to store opening and closing dates along with their respective status of being open or closed
// Function to disable weekend selection
// Configuration options for calendar date and time pickers
// Function to calculate the difference between two selected dates
$scope.$on('$destroy', function() {
that.calculateWatch();
});
} ])
EDIT : Managed to make the calculation work as below:
$scope.calculateUnavailability = function(incident){
var closureDate = incident.closure;
var openDate = incident.opening;
var date1 = Date.parse(closureDate);
var date2 = Date.parse(openDate);
var diff = date1 - date2;
console.log(diff);
var days = Math.floor(diff / 1000 / 60 / 60 / 24);
diff -= days * 1000 * 60 * 60 * 24
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60
var minutes = Math.floor(diff / 1000 / 60);
diff -= minutes * 1000 * 60
var unavailability = 'Difference = ' + days + ' day/s ' + hours + ' hour/s ' + minutes + ' minute/s ';
console.log(unavailability);