I have a this text:
2020-03-12T12:43:11.518+01:00
How can I convert that to datetime? It is probably a jsonformatted datetime. I know we have ISO-DATE, but suddenly I could not find a easy way to convert it back to datetime....
def var myDateTime as datetime no-undo.
myDatetime = System.DateTime:parser('2020-03-12T12:43:11.518+01:00').
It is an ISO 8601 date-time, which is indeed what json uses. One easy way to convert it back is to use what's already available in the Json classes:
USING Progress.Json.ObjectModel.JsonObject. def var ojson as JsonObject no-undo. def var dt as datetime-tz no-undo. ojson = new JsonObject(). ojson:add( "iso", "2020-03-12T12:43:11.518+01:00" ). message ojson:getdatetimetz( "iso" ) skip ojson:getdatetime( "iso" ) skip ojson:getdate( "iso" ) view-as alert-box.
def var myDateTime as datetime no-undo.
myDatetime = System.DateTime:parser('2020-03-12T12:43:11.518+01:00').
It is an ISO 8601 date-time, which is indeed what json uses. One easy way to convert it back is to use what's already available in the Json classes:
USING Progress.Json.ObjectModel.JsonObject. def var ojson as JsonObject no-undo. def var dt as datetime-tz no-undo. ojson = new JsonObject(). ojson:add( "iso", "2020-03-12T12:43:11.518+01:00" ). message ojson:getdatetimetz( "iso" ) skip ojson:getdatetime( "iso" ) skip ojson:getdate( "iso" ) view-as alert-box.
Stefan's JSON solution is the easiest, by far.
If you can't (or don't want to) use JSON, you'll have to do something like this: https://github.com/consultingwerk/ADE-Sourcecode/blob/853518c3945fafcd1f032388d5f77e7a3d76216c/src/corelib/OpenEdge/Core/TimeStamp.cls#L142 .
Note that when you convert the ISO-DATE string into a DATETIME_TZ, you need to (temporarily) set the SESSION:DATE-FORMAT to ymd .
Strange, I added my solution for some time ago, but it does not seems to be registrated. Anyway my solution was to use:
myDatetime = System.DateTime:Parser('2020-03-12T12:43:11.518+01:00')..
Pretty simple, but works only for windows…. I was thinking of doing the jsonway, but since I am on a Microsoft platform…..
I like to use:
session:date-format = "ymd".
display datetime-tz( replace( "2020-03-12T12:43:11.518+01:00", "T", " " )).
Replacing the "T" does not seem to be necessary at least with 11.7+. It might have been needed at some point in the past?
I vaguely recall that some ancient legacy version of datetime-tz barfed if the "T" was present. But that could be my imagination... I just tested all the way back to 10.2b without it being a problem. So it might just be my fevered imagination.
ROFL
No actual fever present at this location. All recommended precautions are being taken. Then again "social distancing" is my preferred way of life :)