Select to view content in your preferred language

Trying to display time in a readable form, works in preview, but when published get Invalid Date error.

354
9
Jump to solution
a week ago
Bec
by
Regular Contributor

Hi!  I'm using Survey123 Connect version 3.21 and have two time only fields that I am trying to convert the output into a readable text field for the end user.  The formulas I am using in my calculation fields work in the Survey123 Connect preview, but once the survey is published, and I input my time values, I get an Invalid Date error.  My formulas are based off of what was in this article from ESRI Support:  How To Display time in readable format when concatenated with text values in ArcGIS Survey123 Connec...

This is the calculation used for the beginhour_text field:  

concat((if(format-date(${time1begin},'%h')>12,(format-date(${time1begin},'%h')-12),(format-date(${time1begin},'%h')))),':',(string(format-date(${time1begin}, "%M"))),(if(format-date(${time1begin},'%h')<12," AM"," PM")))

 

This is the calculation used for the endhour_text field:

concat((if(format-date(${time1end},'%h')>12,(format-date(${time1end},'%h')-12),(format-date(${time1end},'%h')))),':',(string(format-date(${time1end}, "%M"))),(if(format-date(${time1end},'%h')<12," AM"," PM")))

 

This is the calculation used to bring both begin and end time together as timevalue1 for use in the Restrictions applied section of the Director's Order:

if(${time1}='All Times', 'All Times', concat(${beginhour_text}, ' - ',${endhour_text}))

 

Time Fields in Survey123 Connect PreviewTime Fields in Survey123 Connect PreviewTime Output in Survey123 Connect Preview Director's Order SectionTime Output in Survey123 Connect Preview Director's Order Section

Invalid Date Error Showing in Published Survey for Same Times Shown in Survey123 Connect PreviewInvalid Date Error Showing in Published Survey for Same Times Shown in Survey123 Connect Preview

Time Fields in Survey123 Connect Excel DocumentTime Fields in Survey123 Connect Excel Document

I am not sure what the issue is to cause the error, but any help is greatly appreciated!  Thanks!

 

Bec

0 Kudos
1 Solution

Accepted Solutions
Neal_t_k
MVP Regular Contributor

My guess is this has to do with difference in the way that the field app/Connect and the web app handle date calculations and that the Time question is stored as a string and not actually dateTime.   While the field app/connect can handle time as a format-date, the web app is looking for actual dateTime data.

A couple options to ponder.

1) can you change the question types to dateTime instead of Time only?  You could then extract the time easier.  

2) Have you considered a text question with an input mask and regex for the time entry.  It wouldn't have the picker, but you could standardize the input and use the substring formula if you need to split out certain parts.

body::esri:inputMask: 99:99 >A>A

constraint: regex(.,'(((0[1-9])|(1[0-2])):([0-5])([0-9])\s(A|P)M)')

View solution in original post

9 Replies
VenkataKondepati
Regular Contributor

You’re running into a classic Survey123 Preview vs Published runtime mismatch — in Connect Preview, format-date() is forgiving, but once published, time-only fields don’t carry a date context, so format-date() fails and throws Invalid Date. The fix is simple: don’t use format-date() on time fields; use format-time() instead or stick to pure string-based formatting. Bottom line: preview works, production is strict — remove date functions from time-only fields and the issue disappears.

Regards,
Venkat
0 Kudos
Bec
by
Regular Contributor

@VenkataKondepati   If you follow the article, they used a Time only field with format-date in the concatenations to get a readable time output.  I tried your idea of replacing format-date with format-time and Survey123 Connect throws a validation error stating it cannot handle format-time.

Bec

0 Kudos
VenkataKondepati
Regular Contributor


Please check URL below.
How To: Display Time in Readable Format When Concatenated with Text Values in ArcGIS Surve


The Esri workaround is to use format-date() even for time fields.

What to do instead (works)

Use format-date() to format the time value:

concat(${my_id}, ' ', format-date(${my_time}, '%H:%M'))


If you need seconds:

format-date(${my_time}, '%H:%M:%S')

If your input is a time question and you still see epoch sometimes

Convert via dateTime() first, then format:

format-date(dateTime(${my_time}), '%H:%M')


The format-time() isn’t implemented in Survey123 Connect, so stick with format-date() (or format-date(dateTime())) for readable time output.

Please let me if this helps.

Regards,
Venkat
0 Kudos
Bec
by
Regular Contributor

@VenkataKondepati That is the same article I pointed to in my original post that I used to build the calculation formulas for my survey time fields.  And like I said, they work as expected in Preview, but not when published.

0 Kudos
VenkataKondepati
Regular Contributor

Sorry, I missed it. Thanks for clarifying.
This is strange. Maybe we can get on a call if you have time now.

Regards,
Venkat
0 Kudos
Bec
by
Regular Contributor

@VenkataKondepati No worries.  I was leaving work when I saw your reply.  I appreciate all the help on this issue.  @Neal_t_k was able to provide a good work around since the issue is more within how the survey in a web browser interacts with that field vs using the Survy123 app on a phone or tablet.  Thanks again!

Bec

Neal_t_k
MVP Regular Contributor

My guess is this has to do with difference in the way that the field app/Connect and the web app handle date calculations and that the Time question is stored as a string and not actually dateTime.   While the field app/connect can handle time as a format-date, the web app is looking for actual dateTime data.

A couple options to ponder.

1) can you change the question types to dateTime instead of Time only?  You could then extract the time easier.  

2) Have you considered a text question with an input mask and regex for the time entry.  It wouldn't have the picker, but you could standardize the input and use the substring formula if you need to split out certain parts.

body::esri:inputMask: 99:99 >A>A

constraint: regex(.,'(((0[1-9])|(1[0-2])):([0-5])([0-9])\s(A|P)M)')

Bec
by
Regular Contributor

@Neal_t_k Your response was helpful!  It is definitely an issue with how the time is handled when using the survey in a web browser versus using it in the Survey123 app on your phone or tablet.  Unfortunately changing it to dateTime didn't fix the issue as I still got the same Invalid Date error when using the survey in a web browser, regardless if I was using Chrome or Edge.  If they were going to use just the Survey123 app on their phone or tablet, this wouldn't be an issue, but since they will be mostly submitting via a web browser from their laptops, I used your second option of creating just a text question for the time using the mask and constraints.  Thanks again for the insight and help!

 

Bec

0 Kudos
Neal_t_k
MVP Regular Contributor

@Bec I have a couple more solutions for you for the web app.  The idea is to separate out each portion of the the time and then concat it back together.

Using datetime:

Hours:

if(format-date(date-time(decimal-date-time(${time1})), "%H")>12, format-date(date-time(decimal-date-time(${time1})), "%H")-12, if(format-date(date-time(decimal-date-time(${time1})), "%H")=0,12,format-date(date-time(decimal-date-time(${time1})), "%H")))

Mins:

format-date(date-time(decimal-date-time(${time1})), "%M")

AM/PM:

if(format-date(date-time(decimal-date-time(${time1})), "%H")>11, 'PM', 'AM')

 

Using a time only 

hours:

if(substr(${time2}, 0, 2) > 12, int(substr(${time2}, 0, 2))-12, if(substr(${time2}, 0, 2) = 0,12,  substr(${time2}, 0, 2)))

Separator and Minutes:

substr(${time2}, 2, 5)

AM/PM

if(substr(${time2}, 0, 2) > 11, ' PM', ' AM')

 

Then just concat the parts back together.

The time only solution does not work in the field app/connect

The solution using "time only" I improved from @JenniferAcunto 's solution here: https://community.esri.com/t5/arcgis-survey123-questions/formatting-time-into-12-hour-with-am-pm/td-...

I added another "if" to the hours to handle 12 am (zero hour) and in the am/pm calc, needed to be >11 instead of 12.

 

0 Kudos