Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkDateTime.pb"
IncludeFile "CkDtObj.pb"

Procedure ChilkatExample()

    ; Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
    strTimestamp.s = "2016-11-11T14:32:17.0908971Z"

    dateTime.i = CkDateTime::ckCreate()
    If dateTime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromTimestamp(dateTime,strTimestamp)

    ; Get a DtObj in the local timezone.
    bLocalTimezone.i = 1
    dt.i = CkDtObj::ckCreate()
    If dt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckToDtObj(dateTime,bLocalTimezone,dt)

    ; Get the individual date/time components
    Debug "-- Local Time --"
    Debug "Year: " + Str(CkDtObj::ckYear(dt))
    Debug "Month: " + Str(CkDtObj::ckMonth(dt))
    Debug "Day: " + Str(CkDtObj::ckDay(dt))
    Debug "Hour: " + Str(CkDtObj::ckHour(dt))
    Debug "Minutes: " + Str(CkDtObj::ckMinute(dt))
    Debug "Seconds: " + Str(CkDtObj::ckSecond(dt))

    ; Get a DtObj in the GMT/UTC timezone.
    bLocalTimezone = 0
    CkDateTime::ckToDtObj(dateTime,bLocalTimezone,dt)

    ; Get the individual date/time components
    Debug "-- UTC Time --"
    Debug "Year: " + Str(CkDtObj::ckYear(dt))
    Debug "Month: " + Str(CkDtObj::ckMonth(dt))
    Debug "Day: " + Str(CkDtObj::ckDay(dt))
    Debug "Hour: " + Str(CkDtObj::ckHour(dt))
    Debug "Minutes: " + Str(CkDtObj::ckMinute(dt))
    Debug "Seconds: " + Str(CkDtObj::ckSecond(dt))


    CkDateTime::ckDispose(dateTime)
    CkDtObj::ckDispose(dt)


    ProcedureReturn
EndProcedure