Delphi FMX: Date Formatting

Delphi FMX: Date Formatting

The little things that break you REST Web API calls

·

2 min read

This is simple note on Delphi cross-platform development and the importance of proper date formatting. Different platforms are going to provide different results when taking a date object and formatting it to a string using the DateToStr function. It is imperative to keep this in mind.

We are going to dive into a real-world example where this issue can create a headache is interacting with the FitBit Web API. The API requires the application to make calls using a date in the format 'yyyy-MM-dd' to retrieve user data. This all worked fine when developing the Windows version of the application, then on the mobile Android application it worked well for one API call and returned an error for the next call as shown below:

HTTP/1.1 500 Internal Error

The developers at FitBit were taking into account this issue for the steps API call (https://api.fitbit.com/1/user/-/sleep/date/23-01-08.json). However, to access the activities API call it broke! (https://api.fitbit.com/1/user/-/activities/date/23-02-08.json)... as I encounter this, I ask myself: "What gives???"... During testing using the REST Dugger from RAD Studio it works fine... or so it seemed (the audio was shut off, and the Response data was cached from a previous call. At this point I have the Android application failing, the Windows application working, and the Response from the REST Debugger looks as if it is returning a correct result. Finally, after stepping through the code while running on Debug mode in Android, and having turned the speakers on, I was able to narrow down the culprit to the date formatting. It turns out that on Android you will get a converted date string of 1/8/23 and on Windows you will get 1/8/2023. A big faceplant onto the table, after this I am not assuming any date consistencies within different platforms and strictly using the TFormatSettings to force how I need to extract a TDateTime object into a string. The code below shows how to quickly do it:

Hope this saves this helps on your debugging journey!

Did you find this article valuable?

Support Alessandro by becoming a sponsor. Any amount is appreciated!