TDateTime Field throws TJsonTypeConverter.EConvertFromJsonError when providing ISODateString

Hi,

I have used Data Modeler to create the db-entities for creating a REST API to a mysql database. Everything is working fine, except when I want to update fields in the database which are datetime fields.
The column is defined in the class like this:

    [Column('endeZeitpunkt', [])]
    FendeZeitpunkt: Nullable<TDateTime>;

In the TableDictionary class the column is defined like this:

FendeZeitpunkt: TDictionaryAttribute;

and the property looks like this:

property endeZeitpunkt: TDictionaryAttribute read FendeZeitpunkt;

the constructor initializes the field like this:

FendeZeitpunkt := TDictionaryAttribute.Create('endeZeitpunkt');

All endpoints work as expected and I can add, delete and update the data. There is only one issue with the datetime fields. They don't like the ISO-Strings which I produce with the Angular Frontend.

If I provide the value "endeZeitpunkt":"2022-05-18T00:14" all is fine and the patch operation finishes successfully.
However if I provide the value "endeZeitpunkt":"2022-05-17T22:14:00.000Z" then a patch throws the error message as the response:

{
  "error": {
    "code": "TJsonTypeConverter.EConvertFromJsonError",
    "message": "Cannot read JSON property of type \"TDateTime\", invalid value."
  }
}

What am I missing here? Any suggestion is highly appreciated. I have to code around that in the angular frontend and I wonder if I did something wrong when creating the xdata service.

By default, XData doesn't process ISO dates with time zone. It raises an error. You can change that by setting a global parameter to tell how XData should process the time zone:

uses Bcl.Utils;

DefaultTimeZoneMode := TTimeZoneMode.AsLocal;

The above will tell XData to interpret all dates with time zones and convert them to local time zone.

Hi Wagner,

thanks very much for the tip. It works like a charm. If no Timezone Info is provided, it works like before and if a Timezone Info is provided (I only tried Z at the end) the DateTime is converted to the local time.

In my humble opinion, this should be mentioned somewhere in the documentation. For example in the section JSON Format where the Property Values are listed. For the Data Type DateTime, you could put hat info in the Description cell.

Anyway. Problem is solved.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.