· Jul 27, 2022

How to convert from internal date format to web format

How to convert from internal date format to JSON/Web date format: YYYY-MM-DDTHH:mm:ss (example: 2012-04-23T18:25:43)?

Product version: IRIS 2022.1
Discussion (3)2
Log in or sign up to continue

The ObjectScript $ZDATETIME function (also-know-as $ZDT) contains lots of options, some of which are close to what your want.  [[ Note $HOROLOG is also-known-as $H; $ZTIMESTAMP is aka $ZTS. ]]

$ZDT($H,3,1) gives the current local time, where character positions 1-10 contain the date you want and positions 12-19 contain the time you want.  However, character position 11 contains a blank, " ", instead of a "T".

$ZDT($ZTS,3,1) gives the current UTC time with character position 11 containing a blank.

    SET $EXTRACT(datetime,11)="T"
to your datetime result will fix the character position 11 issue.

Instead of using time format 1, you can use time formats 5  and 7 with $H.  $ZDT($H,3,5) gives local time in the format you want except character positions 20-27 contain the local offset from UTC.  $ZDT($H,3,7) converts the local $H date-time to UTC date-time and makes character position 20 contain "Z" to indicate the "Zulu" UTC time zone.  However, if your local time-zone includes a Daylight Saving Time (DST) offset change when DST "falls back" causing a local hour to be repeated then the time format 5 UTC offset or the time format 7 conversion from local to UTC will probably be incorrect during one of those repeated hours.

@Yuri Marx 

//adapted from (HS.FHIRServer.Utils).ConvertToUTC

#dim tSecondsPerDay = 86400

// time with offset
set tTimeWithOffset = $ZDATETIME($HOROLOG,3,5)

if (tTimeWithOffset [ "-") { set tOffsetSign = "-" }
    elseif (tTimeWithOffset [ "+") { set tOffsetSign = "+" }
    else { set tOffsetSign = "" }
if (tOffsetSign'="") {
    // compute offset in seconds
    set tOffset = $P(tTimeWithOffset,tOffsetSign,2)
    set tOffsetHrs = $P(tOffset,":")
    set tOffsetMins = $P(tOffset,":",2)
    set tOffsetInSeconds = (3600*tOffsetHrs) + (60*tOffsetMins)
    // get date/time in $H format
    set tTime = $P(tTimeWithOffset,tOffsetSign)
    set tDateTimeH = $ZDTH(tDate_"T"_tTime,3,1)
    set tDateH = $P(tDateTimeH,",")
    set tTimeH = $P(tDateTimeH,",",2)
    // UTC time = timestamp value (+/-) offset
    // might have to increment or decrement the date
    set tUTCDateH = tDateH
    if (tOffsetSign = "-") {
        set tUTCTimeH = tTimeH + tOffsetInSeconds
        if (tUTCTimeH >= tSecondsPerDay) {
            set tUTCDateH = tUTCDateH+1
            set tUTCTimeH = tUTCTimeH-tSecondsPerDay
    else { 
        set tUTCTimeH = tTimeH - tOffsetInSeconds
        if (tUTCTimeH < 0) {
            set tUTCDateH = tUTCDateH-1
            set tUTCTimeH = tUTCTimeH+tSecondsPerDay

    Set pDateTimeH = tUTCDateH_","_tUTCTimeH
    quit $TR($ZDT(pDateTimeH,3,1)," ","T")_"Z"