Select to view content in your preferred language

Unique help with AM/PM

359
6
Jump to solution
07-08-2025 02:12 PM
Alyciacamille
Emerging Contributor

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!:

typenamelabelcalculationrelevant
select_one update_droprefresh_dropDrop Time:  
calculatedrop_timeDrop Timeonce(format-date(now(), '%n/%e/%Y %H:%M:%S'))${refresh_drop}="record"
notecamera_drop_time${drop_time}  
1 Solution
6 Replies
Leena_Abdulqader
Frequent Contributor

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!🤠

Alyciacamille
Emerging Contributor

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 🙂

Leena_Abdulqader
Frequent Contributor

Bummer! I'm so sorry, 🙁 have you tried what Neal shared below? 

abureaux
MVP Frequent Contributor

This is indeed the solution. I won't mark as the solution as the OP hasn't responded back yet though.

0 Kudos
Alyciacamille
Emerging Contributor

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:

typenamelabelcalculationrelevant
calculatetransect_start_raw once(now())${refresh_start}="record"
select_one update_startrefresh_startStart Time:  
calculatetransect_start_timeTime Startedconcat(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"
notestart_time${transect_start_time}