Select to view content in your preferred language

How to retrieve seconds from datetime function?

1259
6
Jump to solution
04-03-2023 12:02 PM
AshleyN
New Contributor II

We use survey123 for field teams to collect data while in the field, and seconds are crucial for some of our surveys.

I need to get a value for seconds when field teams click the prompt for the datetime type function (would need MON DD YYYY HH:MM:SS). It seems to only record MON DD YYYY HH:MM, with no seconds value. I have been unsuccessful in finding information about whether the second value is generated (and just rounded to minutes after it's retrieved), or not generated at all. Is there a way that I can get this function to provide the seconds, or to generate the seconds when that button is pressed (in another function), and add the seconds on to the generated value?

3 Solutions

Accepted Solutions
Vinzafy
Occasional Contributor

Unfortunately I haven't had much experience with reporting seconds for surveys, but I did a bit of testing in my sandbox environment.

As you experienced, the now() function captures the date and time for when the survey is first triggered so though it could work for the start time, it wouldn't work for the end time. Additionally, now() seems to round up to the closest minute and doesn't record seconds.

However when a user interacts with a dateTime field, it should default to the date and time they triggered that field down to the millisecond (GIF below). So when a user is starting the survey, you could instruct them to tap the dateTime question which would automatically record the date and time. A conditional note field could appear to remind them to trigger the field.

Then when they're finished, have the user interact with another dateTime question for end time (making it required would likely help) which would then record the end date and time to the millisecond.

I'm sure there are better solutions out there, but unfortunately that's all I can think of for the time being.

GifMaker_20230404082544694.gif

 

View solution in original post

AshleyN
New Contributor II

That's interesting! I've been having it work the opposite way, with the dateTime question type always returning 00 in the seconds place, and the now() calculation returning true values for the seconds place (but only generates a value for the instant that the survey is opened).

 

I've tried:

typenamelabelcalculation
dateTimedate_time_startdate_time_start 
notedate_time_start_displaydate_time_start_displayformat-date-time(${date_time_start},'%Y/%m/%d %H:%M:%S')

 

or with note being a "calculation" question type. But that always gives me 00 in the seconds spot. 

 

now() seems to give me true seconds (such as 34, not 00), but only once. Odd! 

 

I kind of found a workaround in using:

 Survey tab  
typenamelabelcalculation
select_one date_time_twodate_time_twodate_time_two 
hiddendate_time_two_calcdate_time_two_calcif(${date_time_two} = 'Get time',now(),0)
notedate_time_two_displaydate_time_two_displayformat-date-time(${date_time_two_calc},'%Y/%m/%d %H:%M:%S')
    
 choices tab  
list_namenamelabel 
date_time_twoGet timeGet time 

 

This unfortunately makes me have to create a separate button from the dateTime question, but does generate the seconds value for me.

View solution in original post

AshleyN
New Contributor II

I think we have a good solution between two things here.. the above and this:

I talked with another person on our team about this - we found that the following format returns true seconds values only if you use the survey123 app. If you try to use the web version, you will only get 00. So, if you need to use the web version it's best to opt for creating your own button for this. I think it might also be key to put this in as a text question type, it was being weird for me if I tried to do it as a calculate type previously.

typenamelabelcalculation
dateTimedate_time_startdate_time_start 
textdate_time_start_displaydate_time_start_displayformat-date-time(${date_time_start},'%Y/%m/%d %H:%M:%S')

 

View solution in original post

6 Replies
Vinzafy
Occasional Contributor

Hey there,

I'm not sure if you can have the seconds show in the actual dateTime question, however you can retrieve the seconds (and milliseconds) from a dateTime question.

The more straightforward way seems to be adding a calculate or note question and having the calculation be

format-date(${dateTime}, '%Y-%m-%d at %H:%M:%S.%3')

Or whatever format you prefer. %S is 0-padded second and %3 is 0-padded millisecond ticks (000-999) as seen in the documentation (scroll down to date formatting). This would store the value as a text field, but the raw dateTime value should be able to be retrieved from the dateTime question if needed.

Note that this doesn't seem to work for time questions as the seconds defaults to zero.

 

Vinzafy_0-1680553374617.png

 

 

There does seem to be another way using scripts which I haven't tried out, but it's mentioned in the thread found here:

Survey123 Time Field

There's also another thread related to the topic here if it helps:

Display Seconds in Survey123

Hope that helps!

0 Kudos
AshleyN
New Contributor II

Hi,

It helps a little.. I now have a column that contains the date with a seconds spot.. but when doing this calculation with the dateTime functioned entered, it only gives me 00 for the seconds spot. When using the now() function in the calculation, it will only calculate the value once, and keeps the first value even if the dateTime field is cleared and then refilled, or if there is another dateTime field (for an end time), that same value appears for that column as well.

now() seems to get me pretty close.. but is there a way to connect it to the dateTime function so that it calculates this value when the dateTime field is interacted with by the user?

As for the script function, I've attempted that but it doesn't seem to generate anything, as my new seconds column that should get filled is blank.

Thanks

 

0 Kudos
Vinzafy
Occasional Contributor

Unfortunately I haven't had much experience with reporting seconds for surveys, but I did a bit of testing in my sandbox environment.

As you experienced, the now() function captures the date and time for when the survey is first triggered so though it could work for the start time, it wouldn't work for the end time. Additionally, now() seems to round up to the closest minute and doesn't record seconds.

However when a user interacts with a dateTime field, it should default to the date and time they triggered that field down to the millisecond (GIF below). So when a user is starting the survey, you could instruct them to tap the dateTime question which would automatically record the date and time. A conditional note field could appear to remind them to trigger the field.

Then when they're finished, have the user interact with another dateTime question for end time (making it required would likely help) which would then record the end date and time to the millisecond.

I'm sure there are better solutions out there, but unfortunately that's all I can think of for the time being.

GifMaker_20230404082544694.gif

 

AshleyN
New Contributor II

That's interesting! I've been having it work the opposite way, with the dateTime question type always returning 00 in the seconds place, and the now() calculation returning true values for the seconds place (but only generates a value for the instant that the survey is opened).

 

I've tried:

typenamelabelcalculation
dateTimedate_time_startdate_time_start 
notedate_time_start_displaydate_time_start_displayformat-date-time(${date_time_start},'%Y/%m/%d %H:%M:%S')

 

or with note being a "calculation" question type. But that always gives me 00 in the seconds spot. 

 

now() seems to give me true seconds (such as 34, not 00), but only once. Odd! 

 

I kind of found a workaround in using:

 Survey tab  
typenamelabelcalculation
select_one date_time_twodate_time_twodate_time_two 
hiddendate_time_two_calcdate_time_two_calcif(${date_time_two} = 'Get time',now(),0)
notedate_time_two_displaydate_time_two_displayformat-date-time(${date_time_two_calc},'%Y/%m/%d %H:%M:%S')
    
 choices tab  
list_namenamelabel 
date_time_twoGet timeGet time 

 

This unfortunately makes me have to create a separate button from the dateTime question, but does generate the seconds value for me.

AshleyN
New Contributor II

I think we have a good solution between two things here.. the above and this:

I talked with another person on our team about this - we found that the following format returns true seconds values only if you use the survey123 app. If you try to use the web version, you will only get 00. So, if you need to use the web version it's best to opt for creating your own button for this. I think it might also be key to put this in as a text question type, it was being weird for me if I tried to do it as a calculate type previously.

typenamelabelcalculation
dateTimedate_time_startdate_time_start 
textdate_time_start_displaydate_time_start_displayformat-date-time(${date_time_start},'%Y/%m/%d %H:%M:%S')

 

Vinzafy
Occasional Contributor

Interesting! It is weird how there are discrepancies at times between S123 App and WebForms at times. Thanks for the update and glad to hear you have a workaround!

0 Kudos