Hello Community,
I've been developing a survey in connect for field data collection. We're deploying an underwater camera system where I need to record the time multiple times down to the second. That was a bit of a headache but found help figuring that out by creating a button. Now I've hit another wall where its displayed in 24 hr time and I can't figure out how to change it to 12 hr AM/PM time so that the data export can match our existing access database. Not the end of the world if I can't get it to work, just trying to avoid as much post processing as possible. Here's a sample of what I have on my spreadsheet, any help is appreciated!:
type | name | label | calculation | relevant |
select_one update_drop | refresh_drop | Drop Time: | ||
calculate | drop_time | Drop Time | once(format-date(now(), '%n/%e/%Y %H:%M:%S')) | ${refresh_drop}="record" |
note | camera_drop_time | ${drop_time} |
Solved! Go to Solution.
Hello @Alyciacamille, You're almost there! I believe Survey123 uses Java’s SimpleDateFormat rules under the hood so if we followed the same rules in your calculation field, instead of using %H, we should use %I (capital i) instead for hour (01–12) format, and at the end add %p for AM/PM marker.
once(format-date(now(), '%m/%d/%Y %I:%M:%S %p'))
Please let me know if that works, it sounds exciting!🤠
Thank you so much for the quick response and help! Sadly, I'm getting an error converting XLSForm message: b"ODK Validate Errors:\n>> Something broke the parser. See above for a hint.\nError evaluating field 'transect_end_time': unrecognized escape in date format string [%I]\nCaused by: java.lang.RuntimeException: unrecognized escape in date format string [%I]\n\t... 10 more\n\nResult: Invalid"
I'll keep tinkering around 🙂
Bummer! I'm so sorry, 🙁 have you tried what Neal shared below?
This is indeed the solution. I won't mark as the solution as the OP hasn't responded back yet though.
Thank you! I had come across these posts but thought I was too much of a beginner to get it to work. Here's my final code if anyone happens to need to record date and time in a 12 hr format down to the second:
type | name | label | calculation | relevant |
calculate | transect_start_raw | once(now()) | ${refresh_start}="record" | |
select_one update_start | refresh_start | Start Time: | ||
calculate | transect_start_time | Time Started | concat(format-date(${transect_start_raw}, '%m/%d/%Y'), ' ', if(int(format-date(${transect_start_raw}, '%H')) < 12, concat(if(int(format-date(${transect_start_raw}, '%H')) = 0, '12', string(int(format-date(${transect_start_raw}, '%H')))), ':', format-date(${transect_start_raw}, '%M'), ':', format-date(${transect_start_raw}, '%S'), ' AM'), concat(if(int(format-date(${transect_start_raw}, '%H')) = 12, '12', string(int(format-date(${transect_start_raw}, '%H') - 12))), ':', format-date(${transect_start_raw}, '%M'), ':', format-date(${transect_start_raw}, '%S'), ' PM'))) | ${refresh_start}="record" |
note | start_time | ${transect_start_time} |