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.
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.
Here is what I tried so far for the conversion:
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 } }]
}
);