Wednesday, March 25, 2009

Converting military time to AM PM format

Below is the code for changing time format from military format to AM PM format. Lot of databases keep the time in HH24:MI format e.g. 14:20 .. but the users may want it to display in user friendly format like 2:20 PM.. so here goes the code to re-format.

If RTrim ($MILITARY_TIME, $Space) <> $Null

Let $HH_TIME = SubStr ($MILITARY_TIME, 1, 2)
Let $MI_TIME = SubStr ($MILITARY_TIME, 4, 2)

Let #HH_TIME = To_Number ($HH_TIME)

If #HH_TIME <>
let $AMPM= ' AM'
else
let $AMPM= ' PM'
End-If

If #HH_TIME = 0
let #HH_TIME = 12
End-If

If #HH_TIME > 12
let #HH_TIME = #HH_TIME - 12
End-If

Let $HH_TIME = To_Char (#HH_TIME)

Let $AM_PM_TIME = $HH_TIME ':' $MI_TIME $AMPM

End-If



2 comments: