ISO-DATE convert

Posted by goo on 18-Mar-2020 11:07

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....

Posted by goo on 18-Mar-2020 12:18

def var myDateTime as datetime no-undo.

myDatetime = System.DateTime:parser('2020-03-12T12:43:11.518+01:00').

Posted by Stefan Drissen on 18-Mar-2020 12:53

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.

All Replies

Posted by goo on 18-Mar-2020 12:18

def var myDateTime as datetime no-undo.

myDatetime = System.DateTime:parser('2020-03-12T12:43:11.518+01:00').

Posted by Stefan Drissen on 18-Mar-2020 12:53

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.

Posted by Peter Judge on 18-Mar-2020 13:10

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 .

Posted by goo on 18-Mar-2020 13:16

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…..

Posted by ChUIMonster on 18-Mar-2020 13:58

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?

Posted by Peter Judge on 18-Mar-2020 16:10

Why the T replacement?
 
It works just fine without that ...
 

Posted by ChUIMonster on 18-Mar-2020 17:52

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.

Posted by Mike Fechner on 18-Mar-2020 17:54

Be careful with the term _fevered_ these days, friend.

Posted by ChUIMonster on 18-Mar-2020 17:56

ROFL

No actual fever present at this location.  All recommended precautions are being taken.  Then again "social distancing" is my preferred way of life :)

This thread is closed