· Jan 26, 2022

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
Discussion (9)2
Log in or sign up to continue

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