Question Norman W. Freeman · Jan 26, 2022

How to get current date and time in YYYYMMDDHHMMSS format ?

Go to the original post@Norman W. Freeman

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

Comments

Anusri Bairi · Jan 27, 2022

Hello,

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 !

Thanks,

Anusri

0
Eduard Lebedyuk · Jan 27, 2022

Use TO_CHAR:

write $tr($SYSTEM.SQL.Functions.TOCHAR($h, "YYYYMMDD HH mi ss"), " ")
0
Vitaliy Serdtsev  Jan 27, 2022 to Eduard Lebedyuk

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.

0
Vitaliy Serdtsev · Jan 27, 2022
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")," ","")
or
$tr($system.SQL.TOCHAR($h,"YYYY^MM^DD^HH24^MI^SS"),"^","")
0
Jeffrey Drumm · Jan 27, 2022
write ##class(Ens.Util.Time).FormatDateTime("%Y%m%d%H%M%S",,$ZDT($ZTS,3))

Assumes you have Ensemble/Interoperability, though ...

0
Julius Kavay · Jan 27, 2022

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

0
Robert Cemper  Jan 27, 2022 to Julius Kavay

factor 64.7 times faster yes

0
Yaron Munz · Jan 28, 2022

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

0
Julius Kavay  Jan 28, 2022 to Yaron Munz

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

0