Select to view content in your preferred language

Indicator Arcade Expression

161
3
a week ago
GaryMorris
Occasional Contributor

I am creating a dashboard using a feature service.  I have to text fields that have dates in dd/mm/yyyy format.  I am trying to write an expression to convert the text field to a date filed and then find the amount of days between the two dates.

 

Any help is greatly appreciated.

0 Kudos
3 Replies
EmilyGeo
Esri Regular Contributor

hi @GaryMorris

This support article should help you with the conversion from string field to date. Then you can use DateDiff to calculate the number of days between the 2 dates. 

 

EmilyGeo_0-1758740248133.png

 

0 Kudos
GaryMorris
Occasional Contributor

Here is what I tried so far for the conversion:

 

var portal = Portal('https://arcgis.com');var fs = FeatureSetByPortalItem(portal,
 'ItemID', 0,['*'], false);

 for (var f in fs){
 var date_Applied = f["Applied_Date"];
 var date_Issued = f["Issued_Date"];
 var convertedDate = Date(date_Applied)
 var convertedDate1 = Date(date_Issued);
 
 }

return convertedDate1
 
 
I am getting a null value.
0 Kudos
KenBuja
MVP Esteemed Contributor

Here's one way to calculate the difference (this shows the number of days since the beginning of the year). Note that the month number in the Date function is 0-based (0 is January)

function StringToDate(input) {
  var arrDate = Split(input, "/");
  return Date(arrDate[2], arrDate[1] - 1, arrDate[0]);
}

var date1 = "01/01/2025";
var date2 = Text(Now(), "DD/MM/Y"); //gives today's date as dd/mm/yyyy
Floor(DateDiff(StringToDate(date2), StringToDate(date1), "days"));

If your goal is to put this number into an indicator, then you have to return a FeatureSet containing that number

function StringToDate(input) {
  var arrDate = Split(input, "/");
  return Date(arrDate[2], arrDate[1] - 1, arrDate[0]);
}

var date1 = "01/01/2025";
var date2 = Text(Now(), "DD/MM/Y"); //gives today's date as dd/mm/yyyy
var days = Floor(DateDiff(StringToDate(date2), StringToDate(date1), "days"));

return FeatureSet(
  {
    fields: [{ name: "Days", type: "esriFieldTypeInteger" }],
    features: [{ attributes: { Days: days } }]
  }
);

 

0 Kudos