If statement in arcade (with date)

153
2
Jump to solution
09-04-2022 09:15 PM
Labels (1)
BronwenHughes
New Contributor III

This is my first time writing an Arcade expression and I'm having trouble!  I have a feature service of koala sightings that is maintained by volunteers. Once they have completed their data entry they select "YES"  from the YES / NO domain assigned to the "Data record complete" (string) field.  They then have to enter the current date in the "Date completed" (date) field.

To streamline their data entry, if "Data record complete" = 'YES'  I would like to auto-populate "Date completed" with today's date.  

In the Field Maps form editor, I have attempted to create a new calculated expression for the "Date completed" field , however it has an error somewhere in the code.:

var recordcomplete = $feature["Data_record_complete"];
var todaydate = Date()
if(recordcomplete = 'YES'){
return todaydate;
}

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor

Your error is in the if statement.

Most programming languages use "=" as assignment (var x = 3) and "==" as check for equal (x == 3 --> true).

if($feature["Data_record_complete"] == "YES") {
    return Now()
}

 

While the default return value of Date() is the current date and time, I would use Now() here. It returns the same value but when reading the code it's clearer what you want to do, as you can construct arbitrary Dates with Date().

Also, depending on your field, you might want to use Today(), which only returns the date without the time.

JohannesLindner_0-1662356100112.png

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Regular Contributor

Your error is in the if statement.

Most programming languages use "=" as assignment (var x = 3) and "==" as check for equal (x == 3 --> true).

if($feature["Data_record_complete"] == "YES") {
    return Now()
}

 

While the default return value of Date() is the current date and time, I would use Now() here. It returns the same value but when reading the code it's clearer what you want to do, as you can construct arbitrary Dates with Date().

Also, depending on your field, you might want to use Today(), which only returns the date without the time.

JohannesLindner_0-1662356100112.png

 


Have a great day!
Johannes
BronwenHughes
New Contributor III

Gosh, that was simple solution thank you!

0 Kudos