DataFlex
DataFlex
How to Parse a TimeStamp (such as 2016-11-11T14:32:17.0908971Z)
Timestamps are frequently used in REST API responses. This example demonstrates how to parse a timestamp string to get at the date/time components in the local timezone or in the GMT/UTC timezone.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
String sStrTimestamp
Handle hoDateTime
Boolean iSuccess
Boolean iBLocalTimezone
Variant vDt
Handle hoDt
Integer iTemp1
// Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
Move "2016-11-11T14:32:17.0908971Z" To sStrTimestamp
Get Create (RefClass(cComCkDateTime)) To hoDateTime
If (Not(IsComObjectCreated(hoDateTime))) Begin
Send CreateComObject of hoDateTime
End
Get ComSetFromTimestamp Of hoDateTime sStrTimestamp To iSuccess
// Get a DtObj in the local timezone.
Move True To iBLocalTimezone
Get Create (RefClass(cComChilkatDtObj)) To hoDt
If (Not(IsComObjectCreated(hoDt))) Begin
Send CreateComObject of hoDt
End
Get pvComObject of hoDt to vDt
Send ComToDtObj To hoDateTime iBLocalTimezone vDt
// Get the individual date/time components
Showln "-- Local Time --"
Get ComYear Of hoDt To iTemp1
Showln "Year: " iTemp1
Get ComMonth Of hoDt To iTemp1
Showln "Month: " iTemp1
Get ComDay Of hoDt To iTemp1
Showln "Day: " iTemp1
Get ComHour Of hoDt To iTemp1
Showln "Hour: " iTemp1
Get ComMinute Of hoDt To iTemp1
Showln "Minutes: " iTemp1
Get ComSecond Of hoDt To iTemp1
Showln "Seconds: " iTemp1
// Get a DtObj in the GMT/UTC timezone.
Move False To iBLocalTimezone
Get pvComObject of hoDt to vDt
Send ComToDtObj To hoDateTime iBLocalTimezone vDt
// Get the individual date/time components
Showln "-- UTC Time --"
Get ComYear Of hoDt To iTemp1
Showln "Year: " iTemp1
Get ComMonth Of hoDt To iTemp1
Showln "Month: " iTemp1
Get ComDay Of hoDt To iTemp1
Showln "Day: " iTemp1
Get ComHour Of hoDt To iTemp1
Showln "Hour: " iTemp1
Get ComMinute Of hoDt To iTemp1
Showln "Minutes: " iTemp1
Get ComSecond Of hoDt To iTemp1
Showln "Seconds: " iTemp1
End_Procedure