I have this code written in Arcade for a data expression to display the maximum, minimum and average waiting times of the most recent 4 years in 3 separate indicators. However, the time values that are outputted as a result of the code are negative. I'm not sure what's causing the negative values as I've looked through the code several times and there's nothing that looks wrong to me. This is what it's outputting.
I've even tried flipping date received with the well abandonment date in line 41 of the code and still got negative values.
Any suggestions to fix this?
// Define the feature set
var Dates = FeatureSetByPortalItem(
Portal('https://regionofpeel.maps.arcgis.com'),
'5d0f25a7a3314686b526bab55653befb',
0
);
// Function to check if a string represents a valid date
function IsDate(dateString) {
// Check if the date string is not null, undefined, or an empty string
if (dateString == null || dateString == "") {
return false;
}
// Attempt to create a Date object from the string using DateTime function
var dateObject = Date(dateString);
// Check if the created Date object is a valid date
return !IsEmpty(dateObject);
}
var i = 0;
var totalWaitingTime = 0;
var maxWaitingTime = -Infinity;
var minWaitingTime = Infinity;
var features = [];
// Calculate the date 4 years ago
var currentDate = Date();
var fourYearsAgo = DateAdd(currentDate, -4, 'years');
// Loop through records in the feature set
for (var record in Dates) {
var wellAbandonmentDate = record['Date_abandonment'];
var dateReceived = record['date_recieved'];
// Check if both dates are valid and within the last 4 years
if (IsDate(wellAbandonmentDate) && IsDate(dateReceived) && wellAbandonmentDate >= fourYearsAgo && dateReceived >= fourYearsAgo) {
// Calculate waiting time in days using the DateDiff function
var waitingTime = DateDiff(dateReceived, wellAbandonmentDate, 'days');
// Update total, max, min waiting times
totalWaitingTime += waitingTime;
// Update max and min waiting times without using operators
maxWaitingTime = Max(maxWaitingTime, waitingTime);
minWaitingTime = Min(minWaitingTime, waitingTime);
// Add feature to Dict
var feat = {
'attributes': {
'WellAbandonmentDate': wellAbandonmentDate,
'DateReceived': dateReceived,
'WaitingTime': waitingTime
}
};
// Replace the features array with the current feature if it has a greater waiting time
if (waitingTime == maxWaitingTime) {
features = [feat];
}
i++;
}
}
// Calculate average waiting time
var averageWaitingTime;
if (i > 0) {
averageWaitingTime = totalWaitingTime / i;
} else {
averageWaitingTime = null;
}
// Loop through features
for (var j in features) {
var featureData = features[j];
// Add calculated values to the features
featureData.attributes.AverageWaitingTime = averageWaitingTime;
featureData.attributes.MaxWaitingTime = maxWaitingTime;
featureData.attributes.MinWaitingTime = minWaitingTime;
}
var Dict = {
'fields': [
{'name': 'AverageWaitingTime', 'type': 'esriFieldTypeDouble'},
{'name': 'MaxWaitingTime', 'type': 'esriFieldTypeDouble'},
{'name': 'MinWaitingTime', 'type': 'esriFieldTypeDouble'}
],
'geometryType': '',
'features': features
};
return FeatureSet(Dict);