Yone Moreno · Aug 18, 2020

Convert date from: 2021-08-18T07:44:14.180+0000 to dd/mm/yyyy


We would need help.

We have used the following commands:

set fecha = "2021-08-18T07:44:14.180+0000"

set nuevaFecha = ##class(Ens.Util.Time).ConvertDateTime(fecha,"%Y-%m-%d%T%H:%M:%S","%d/%m/%Y")


And when we write it to the terminal:

w nuevaFecha


It outputs nothing


Could you help us?


We have read:


0 216
Discussion (3)2
Log in or sign up to continue

Hi Yone.

Notice -- 4th argument is status code of conversion:

ENSDEMO>set fecha = "2021-08-18T07:44:14.180+0000"

ENSDEMO>set nuevaFecha = ##class(Ens.Util.Time).ConvertDateTime(fecha,"%Y-%m-%d%T%H:%M:%S","%d/%m/%Y",,.status)

ENSDEMO>zw status
status="0 "_$lb($lb("<Ens>ErrGeneral","Extra text 'T07' not parsed before end marker ':'",,,,,,,,$lb(,"ENSDEMO",$lb("e^zParseDateTime+102^Ens.Util.Time.1^2","e^zConvertDateTime+3^Ens.Util.Time.1^1","e^^^0"))))

Adjust format and you'll get expected value:

ENSDEMO>set nuevaFecha = ##class(Ens.Util.Time).ConvertDateTime(fecha,"%Y-%m-%dT%H:%M:%S.%N%z","%d/%m/%Y",,.sc)

ENSDEMO>write nuevaFecha

Hey Yone.

There's two issues with your useage of Ens.Util.Time.

  1. Your spec  in is missing the milliseconds and the timezone offset
  2. You have a % before the T which seperates the date and time

The following should work for you:

Set fecha = "2021-08-18T07:44:14.180+0000"

Set nuevaFecha = ##class(Ens.Util.Time).ConvertDateTime(fecha,"%Y-%m-%dT%H:%M:%S.%N%z","%d/%m/%Y")

Write nuevaFecha

Which should output "18/08/2021"

EDIT: And as Alexander points out - the forth argument outputs the status of your calling of the class, which is very useful for debugging these kinda of issues, but you may want to check for the error in your code just in case the source of the date is formatted incorrectly or some other issue occurs.