· Nov 24, 2020

Change date format in an easier way in objectscript

I am getting the date 20201121090000 in the HL7 message, How do I convert it to 2020-11-21 09:00:00 in a easy way?

I am currently doing it by extracting the first 7 values and splitting as date and time and then adding a hyphen using substring.

Is there an easier way by using $ZDATE? or something like that?

Discussion (20)1
Log in or sign up to continue

Hey ED Coder.

There are built in classes to manage this in a nicer way.

##class(Ens.Util.Time).ConvertDateTime() is a good starting point. For example:

Here is the filled in classmethod call for easy copy/pasting:

Set NewDate = ##class(Ens.Util.Time).ConvertDateTime(HL7Date,"%Y%m%d%H%M%S","%Y-%m-%d %H:%M:%S")

The values for each section of the date are defined by the following:

After seeing several solutions I got the idea to make a comparison.
The bottom line is, it's advisable to check how an algorithm (or function or method etc.) performs over another.
So try the below program snippet... you will be surprised!

Test //
   s date="20201121090000"
   s new=""
   s t0=$zh

   f i=1:1:1E6 s new=$e(date,1,4)_"-"_$e(date,5,6)_"-"_$e(date,7,8)_" "_$e(date,9,10)_":"_$e(date,11,12)_":"_$e(date,13,14)
   s t1=$zh
   f i=1:1:1E6 s new=$tr("abcd-ef-gh ij:kl:mn","abcdefghijklmn",date)
   s t2=$zh
   f i=1:1:1E6 s new=$zd($zdh($e(date,1,8),8),3)_" "_$e(date,9,10)_":"_$e(date,11,12)_":"_$e(date,13,14)
   s t3=$zh
   f i=1:1:1E6 s new=$system.SQL.TOTIMESTAMP(date, "YYYYMMDDHHMISS") 
   s t4=$zh
   s t5=$zh

   w "$e() only",?12,t1-t0,!
   w "$tr()",?12,t2-t1,!
   w "$e()+$zd()",?12,t3-t2,!
   w "SQL/class",?12,t4-t3,!
   w "SQL/static",?12,t5-t4,!

Of course, the results will depend on hardware,  on Cache/IRIS version and on utilisation of your system

Results running IRIS Container on Raspberry Pi 4 (4Gb RAM):

$e() only   1.279893

$tr()       1.281999

$e()+$zd()  1.470656

SQL/class   66.405927

I didn't include the SQL/static in my test on the RPi.

Comparison with IRIS on a Windows 10 i7 based Intel NUC machine:

$e() only   .14742
$tr()       .185998
$e()+$zd()  .182579
SQL/class   11.305143

Mind you, the NUC cost about 15 times that of the RPi :-)

Anyway, based on a combination of the performance and coolness of technique, the $tr() one gets my vote!