Question
· Jun 13

DateTime from yyyy-mm-dd hh:mm:ss.000 to yyyymmdd

how do I convert DateTime from yyyy-mm-dd hh:mm:ss.000 to yyyymmdd

Discussion (5)2
Log in or sign up to continue

Hi Krishnaveni


There are 2 answers to this. The easy but wrong way , and the more correct way


The easy way would be to take the piece of the string before the space character, and then remove all nonNumeric chars

set output = $ZSTRIP($PIECE(input," ",1),"*AP")

The better was is to convert to the canonical date format, then convert back to the format you want.  Relevant documentation page: https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

set internaldate = $ZDATETIMEH(input,3)

set outputdate = $ZDATE(internaldate,8)

There's a good thread from a few years ago that goes into various ways of converting date formats which you can find here.

My approach in that thread was to suggest the use of the class method ##class(Ens.Util.Time).ConvertDateTime()

In your case using this option, you could do this for your main question:

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

And then for your followup to include seconds:

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