AGOL Arcade Question Date Difference

4123
18
Jump to solution
02-12-2021 09:19 AM
LukeAllen2
Occasional Contributor

Hi all,

I'm sort of new to coding in general, but I have started trying to use some arcade expressions in my web maps, currently I have a web map which has a date painted field for hydrants and with the arcade expression below I am using the current date and this field to provide a count of the years and days since a hydrant was last painted.

However, I was QC'ing the data and noticed that for hydrants painted after 2020, it is going to be a day short as 2020 was a leap year - if the web map persists beyond 2024, it will then start being 2 days short.

With my limited knowledge I believe that I need to use some type of when or if statement to be able to use 366 rather than 365 (in var d calculation) i'm just now sure how I would go about doing it - would anybody be able to provide some pointers? 

Many thanks!

var timeNow = Now();
var survey = $feature["DATE_PAINTED"];

var years = DateDiff(timeNow, survey, 'Years');
// 2.1116274159012844

var y = Floor(years, 0);
var d = (years - y) * 365;

if (IsEmpty($feature.DATE_PAINTED)){
return "-"
} else if (y >= 2) {
return "Last Painted: " + Round(y, 0) + " years and " + round(d , 0) + " days ago."
} else if (y == 1) {
return "Last Painted: " + Round(y, 0) + " year and " + round(d , 0) + " days ago."
} else if (y == 0) {
return "Last Painted: " + round(d , 0) + " days ago."
}

0 Kudos
3 Solutions

Accepted Solutions
DavidPike
MVP Frequent Contributor

Try changing the inputs about in the arcade playground  ArcGIS Arcade | ArcGIS for Developers and see if this suits:

var a = Date('2021-02-12 00:00:01')
var b = Date('2020-01-14 00:00:01')

var years = Floor(DateDiff(a,b,'years'))
var days = DateDiff(a,DateAdd(b, years, 'years'),'days')

return "Last Painted: " + years + " years and " + days + " days ago."

ArcadeDateDiff.png

View solution in original post

XanderBakker
Esri Esteemed Contributor

Hi @LukeAllen2 ,

My bad. Apparently, there is a line missing in the function. Can you try this?

function daysThisYear() {
    // https://stackoverflow.com/questions/16353211/check-if-year-is-leap-year-in-javascript
    var y = Year(Now());
    
    // check leap year
    if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
        return 366;
    } else {
        return 365;
    }
}

var timeNow = Now();
var survey =  $feature["DATE_PAINT"];
// var survey = Date(2015, 1, 1);
var years = DateDiff(timeNow, survey, 'Years');
var y = Floor(years, 0);
var d = Round((years - y) * daysThisYear(), 0);

if (IsEmpty($feature["DATE_PAINT"])){
    return "-";
} else if (y >= 2) {
    return "Last Painted: " + y + " years and " + Floor(d , 0) + " days ago.";
} else if (y == 1) {
    return "Last Painted: 1 year and " + Floor(d , 0) + " days ago.";
} else if (y == 0 && d >= 2) {
    return "Last Painted: " + Floor(d , 0) + " days ago.";
} else if (y == 0 && d >= 1) {
    return "Last Painted: " + Floor(d , 0) + " day ago.";
} else if (y == 0 && d < 1) {
    return "Last Painted: today.";
}

 

View solution in original post

DavidPike
MVP Frequent Contributor

This is the concept I initially posted, @LukeAllen2  did it not work out?

View solution in original post

18 Replies
DavidPike
MVP Frequent Contributor

that's an interesting one.  Can you get the number of years difference, say 2.11 then e.g. create a new variable where you add 2 years to survey, then compare DateDiff(,,'days') 

0 Kudos
LukeAllen2
Occasional Contributor

var timeNow = Now();
var survey = $feature["DATE_PAINTED"];

var years = DateDiff(timeNow, survey, 'Years');
// 2.1116274159012844

var y = Floor(years, 0);
var d = (years - y) * 365;

if (IsEmpty($feature.DATE_PAINTED)){
return "-"
} else if (y >= 2) {
return "Last Painted: " + Round(y, 0) + " years and " + Round(d , 0) + " days ago."
} else if (y == 1) {
return "Last Painted: " + Round(y, 0) + " year and " + Round(d , 0) + " days ago."
} else if (y == 0 && d > 1) {
return "Last Painted: " + Round(d , 0) + " days ago."
} else if (y == 0 && d <= 1) {
return "Last Painted: " + Round(d , 0) + " day ago."
}

 

Still playing around with it, and getting myself slightly more confused as this point!

When comparing the duration I'm getting from the above code its not quite the same as most online time duration calculators - actually short by 2 days -- is there something obvious that I missing here? (See attachment for context)

0 Kudos
DavidPike
MVP Frequent Contributor

Try changing the inputs about in the arcade playground  ArcGIS Arcade | ArcGIS for Developers and see if this suits:

var a = Date('2021-02-12 00:00:01')
var b = Date('2020-01-14 00:00:01')

var years = Floor(DateDiff(a,b,'years'))
var days = DateDiff(a,DateAdd(b, years, 'years'),'days')

return "Last Painted: " + years + " years and " + days + " days ago."

ArcadeDateDiff.png

LukeAllen2
Occasional Contributor

Hi David, thank you I think your code is alot more stream lined and I'll use that with a few adjustments to catch those that were painted today, yesterday or  within the same year etc.

 

Many thanks !

0 Kudos
DavidPike
MVP Frequent Contributor

Good stuff, In my head it works conceptually,  (let the datetime module do the work - I'm guessing that's what its based on??) A few If Elses for the formatting and should be good.  I hope the concept kinda makes sense.

KenBuja
MVP Esteemed Contributor

You checked the option "Include end date in calculation", which doesn't not make sense in your case. For example, if you calculate the difference between 1/1/2020 and 1/2/2020 with the option checked, it returns 2 even though it's only one day later.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @LukeAllen2 ,

I think this thread is related to what you send me by private message. You can use a function that will determine the number of days in a year (taking into account the leap years):

function daysThisYear() {
    // https://stackoverflow.com/questions/16353211/check-if-year-is-leap-year-in-javascript
    
    // check leap year
    if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
        return 366;
    } else {
        return 365;
    }
}

 

And below the complete expression:

function daysThisYear() {
    // https://stackoverflow.com/questions/16353211/check-if-year-is-leap-year-in-javascript
    
    // check leap year
    if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
        return 366;
    } else {
        return 365;
    }
}

var timeNow = Now();
var survey =  $feature["DATE_PAINTED"];
// var survey = Date(2015, 1, 1);
var years = DateDiff(timeNow, survey, 'Years');
var y = Floor(years, 0);
var d = Round((years - y) * daysThisYear(), 0);

if (IsEmpty($feature.DATE_PAINTED)){
    return "-";
} else if (y >= 2) {
    return "Last Painted: " + y + " years and " + Floor(d , 0) + " days ago.";
} else if (y == 1) {
    return "Last Painted: 1 year and " + Floor(d , 0) + " days ago.";
} else if (y == 0 && d >= 2) {
    return "Last Painted: " + Floor(d , 0) + " days ago.";
} else if (y == 0 && d >= 1) {
    return "Last Painted: " + Floor(d , 0) + " day ago.";
} else if (y == 0 && d < 1) {
    return "Last Painted: today.";
}
LukeAllen2
Occasional Contributor

Hi all, Xanders expression seems to work great in the arcade test area of the expression builder, but when I add it to the popup nothing shows, ive no idea what could be causing that to happen? 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @LukeAllen2 ,

Can you validate if the feature has a painted date and can you see in the developer options of the browser if there is any error message when you click on the feature?

0 Kudos