Datetime with T

I am sending an API request to an external app. The API is returning an object with a date property that is a string in the format for example - 2023-08-02T23:05:52.187
I am not able to convert that datetime to a local date time unless I use some string manipulation like separate by the letter T, break them into characters and build the date and time. I was wondering if there is a standard function in ignition that I can use to convert the above formatted date string (with T in it) to a local date time. Thank you

I'd imagine some variation of the format string provided to system.date.parse would get you there.

https://docs.inductiveautomation.com/display/DOC81/system.date.parse

so far I tried these options. in the script console. Only 1 option without time worked.

datetime_with_t = '2023-08-02T23:05:52.187'
system.date.parse(datetime_with_t, 'yyyy-mm-dd"T"hh:mm:ssZ') - Gives illegalargumentexception for the letter T
system.date.parse(datetime_with_t, 'yyyy-mm-dd-hh:mm:ssZ') - java.text.ParseException: Unparseable date: "2023-08-02T23:05:52.187"
system.date.parse(datetime_with_t,yyyy-mm-dd hh:mm:ssZ) - java.text.ParseException: Unparseable date: "2023-08-02T23:05:52.187"
system.date.parse(datetime_with_t,yyyy-MM-dd'T'00:00:00Z) - java.text.ParseException: java.text.ParseException: Unparseable date: "2023-08-02T23:05:52.187"
system.date.parse(datetime_with_t,yyyy-MM-dd hh:mm:ss) - java.text.ParseException: java.text.ParseException: Unparseable date: "2023-08-02T23:05:52.187"
system.date.parse(datetime_with_t,yyyy-MM-dd) - Result = Wed Aug 02 00:00:00 EDT 2023. success but no time. I need time as well.

Try this:

datetime_with_t = '2023-08-02T23:05:52.187'

system.date.parse(datetime_with_t, "yyyy-MM-dd'T'HH:mm:ss.SSS")

shame on your API for not indicating UTC or timezone...

3 Likes

Grrr. Somebody types too fast.

2 Likes

Thank you so much. I was almost there. LOL

1 Like

For future reference, this is ISO-8601 format, looks like +00:00 offset is implied..

EDIT: should have a Z at the end if it is UTC, though

Yeah, but when returned by an API call... whose local does it use?

My take would be that it is UTC if TZ is omitted from the string. Though, per my edit above, should have a Z on the end in that case.

Yeah, needs the Z. Unspecified/unqualified means local time, and is ambiguous. Stupid for an API.

Ahhh, I actually didn't know that.. String-based date/time representations are evil unless fully-qualified. :smiley:

my bad. The API vendor did mention as UTC in the documentation. I missed it. Thanks

Vendor's bad. They almost followed the international standard but failed at the last character.

2 Likes