Ankur Shah · Oct 12, 2020

Convert numeric expression(202010011000) to datetime format (8/6/2018 9:10 PM)

Hi Team,

I want to convert numeric expression (202010011000) to datetime format(8/6/2018 9:10 PM)


Ankur Shah 

0 5 89 2


Are two of these dates 202010011000 and 8/6/2018 9:10 PM equal?

Or, what the format for the numeric one?

It is more likely 2020-10-01 10:00.

it's easy, without AM/PM

write $tr("abcd-ef-gh ij:kl:00","abcdefghijkl",202010011000),! // ODBC-Format
write $tr("gh.ef.abcd ij:kl:00","abcdefghijkl",202010011000),! // German-Format
write $tr("ef/gh/abcd ij:kl:00","abcdefghijkl",202010011000),! // US-Format

but if you need AM/PM, then the shortest way is using $zd()/$zth() functions

set datetime=202010011000
set tmp=$tr("abcdefgh ij:kl:00","abcdefghijkl",datetime) // or using $e(...)
set tmp=$e(datetime,1,8)_" "_$e(datetime,9,10)_":"_$e(datetime,11,12)_":00" write $zdt($zdth(tmp,3),1,3)

Julius, I enjoy your $tr solution.

As to $zdth, you've just missed dformat argument, so it should be:

set tmp=$e(datetime,1,8)_" "_$e(datetime,9,10)_":"_$e(datetime,11,12)_":00" write $zdt($zdth(tmp,8,3),8,3)

<ILLEGAL VALUE> occurs otherwise.

Thanks for the adjustment, I looked at the very first line (odbc format, which is 3).

Anyway, the $tr() construct is called the $tr()-backwards format, I found this on the internet more then ten years ago... so the credit goes to a unknown inventor.

$tr(targetPattern, sourcePattern, sourceValue) --> targetValue