Question Krishnaveni Kapu · Jun 13, 2024

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

Comments

Chris Stewart · Jun 13, 2024

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

set internaldate = $ZDATETIMEH(input,3)

set outputdate = $ZDATE(internaldate,8)
0
Chris Stewart  Jun 14, 2024 to Krishnaveni Kapu

That time format isn't an inbuilt function for IRIS, so for this, you can adapt the string function above.  Take the piece before the . (as your expected output doesn't include it), then strip all non numeric

set output = $ZSTRIP($PIECE(input,".",1),"*AP")
0
Julian Matthews · Jun 14, 2024

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")

0