Norman W. Freeman · Jan 26

How to get current date and time in YYYYMMDDHHMMSS format ?

I would like to get current date and time in this format : YYYYMMDDHHMMSS  (eg: 20220126155704)

Simplest way to do that I found so far is this : 

$translate($zdatetime($horolog,8,1), " :", "")

It works, but it's not that great (I would like to avoid string manipulation) Is there a better, cleaner way ?

Product version: Caché 2018.1
0 240
Discussion (9)2
Log in or sign up to continue


Please try below, this should help you !

write $zstrip($zdatetime($horolog, 3), "*pw") 

Output :  20220127113314

write $zstrip($zdatetime($horolog, 3), "*p") 

Output :  20220127113314

You can use either of the above two , both works !




write $tr($SYSTEM.SQL.Functions.TOCHAR($h, "YYYYMMDD HH mi ss"), " ")

In Caché 2018.1, which the author indicated, there is no such function.
In addition, instead of HH need to specify HH24, otherwise instead of 20220126155704 will get 20220126035704.

I would like to avoid string manipulation

Delimiters in the format are required, so you will have to manipulate the strings anyway.

$tr($system.SQL.TOCHAR($h,"YYYYMMDD HH24 MI SS")," ","")
write ##class(Ens.Util.Time).FormatDateTime("%Y%m%d%H%M%S",,$ZDT($ZTS,3))

Assumes you have Ensemble/Interoperability, though ...

Your solution is just perfect. And fast.

But yes, you can avoid string manipulations... This one, for example, uses math only, merely it's neither short nor looks elegant:

 set dt=$h write $zd(dt,8)*100+($p(dt,",",2)\3600)*100+($p(dt,",",2)#3600\60)*100+($p(dt,",",2)#60)

but gives the same result as your short and nice solution...

 set dt=$h write $zd(dt,8)*100+($p(dt,",",2)\3600)*100+($p(dt,",",2)#3600\60)*100+($p(dt,",",2)#60),!,$tr($zdt(dt,8)," :")

On the other hand, you can install new brakes on your car, as suggested by others... ;-))

Just compare those codes with yours:

set h=$h, t=$zh for i=1:1:1E6 { set x=$tr($system.SQL.TOCHAR($h,"YYYY^MM^DD^HH24^MI^SS"),"^") } write $zh-t,!

set h=$h, t=$zh for i=1:1:1E6 { set x=$tr($zdt(h,8)," :") } write $zh-t,!

The choice is yours...

Here is a test for running all those codes. 
It looks like that the 1st solution (with $translate) is the fastest.

You forgot to take into account the $system.SQL.* variants . They make much more fun, especially because you get time for a coffee break... devil