Sample code for 30+ languages & platforms
PowerBuilder

JSON Date Parsing

See more JSON Examples

Demonstrates how to parse date/time strings from JSON.

Note: This example uses the DtOf and DateOf methods introduced in Chilkat v9.5.0.73

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Json
oleobject loo_DateTime
oleobject loo_Dt
long li_GetAsLocal

li_Success = 0

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Json
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Json.EmitCompact = 0

//  First, let's create JSON containing some date/time strings.
loo_Json.UpdateString("test.timestamp","2018-01-30T20:35:00Z")
loo_Json.UpdateString("test.rfc822","Tue, 24 Apr 2018 08:47:03 -0500")
loo_Json.UpdateString("test.dateStrings[0]","2018-01-30T20:35:00Z")
loo_Json.UpdateString("test.dateStrings[1]","Tue, 24 Apr 2018 08:47:03 -0500")
loo_Json.UpdateNumber("test.StartLoggingTime","1446834998.695")
loo_Json.UpdateNumber("test.Expiration","1442877512.0")
loo_Json.UpdateInt("test.StartTime",1518867432)

Write-Debug loo_Json.Emit()

//  We've built the following JSON:

//  {
//    "test": {
//      "timestamp": "2018-01-30T20:35:00Z",
//      "rfc822": "Tue, 24 Apr 2018 08:47:03 -0500",
//      "dateStrings": [
//        "2018-01-30T20:35:00Z",
//        "Tue, 24 Apr 2018 08:47:03 -0500"
//      ],
//      "StartLoggingTime": 1446834998.695,
//      "Expiration": 1442877512.0,
//      "StartTime": 1518867432
//    }
//  }

//  Use the DateOf and DtOf methods to load Chilkat date/time objects with the date/time values.
//  The CkDateTime object is primarily for loading a date/time from numerous formats, and then getting
//  the date/time in various formats.  Thus, it's primarly for date/time format conversion.
//  The DtObj object holds a date/time where the individual components (day, month, year, hour, minutes, etc.) are 
//  immediately accessible as integers.
loo_DateTime = create oleobject
li_rc = loo_DateTime.ConnectToNewObject("Chilkat.CkDateTime")

loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.DtObj")

li_GetAsLocal = 0

//  Load the date/time at test.timestamp into the dateTime object.
li_Success = loo_Json.DateOf("test.timestamp",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)
Write-Debug string(loo_DateTime.GetAsUnixTime(0))
Write-Debug loo_DateTime.GetAsRfc822(li_GetAsLocal)

li_Success = loo_Json.DateOf("test.rfc822",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)

loo_Json.I = 0
li_Success = loo_Json.DateOf("test.dateStrings[i]",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)

loo_Json.I = 1
li_Success = loo_Json.DateOf("test.dateStrings[i]",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)

li_Success = loo_Json.DateOf("test.StartLoggingTime",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)

li_Success = loo_Json.DateOf("test.Expiration",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)

li_Success = loo_Json.DateOf("test.StartTime",loo_DateTime)
Write-Debug loo_DateTime.GetAsTimestamp(li_GetAsLocal)

//  Output so far:

//  	2018-01-30T20:35:00Z
//  	1517344500
//  	Tue, 30 Jan 2018 20:35:00 GMT
//  	2018-04-24T13:47:03Z
//  	2018-01-30T20:35:00Z
//  	2018-04-24T13:47:03Z
//  	2015-11-07T00:36:38Z
//  	2015-09-22T04:18:32Z
//  	2018-02-17T17:37:12Z

//  Now load the date/time strings into the dt object:
li_Success = loo_Json.DtOf("test.timestamp",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

li_Success = loo_Json.DtOf("test.rfc822",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

loo_Json.I = 0
li_Success = loo_Json.DtOf("test.dateStrings[i]",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

loo_Json.I = 1
li_Success = loo_Json.DtOf("test.dateStrings[i]",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

li_Success = loo_Json.DtOf("test.StartLoggingTime",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

li_Success = loo_Json.DtOf("test.Expiration",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

li_Success = loo_Json.DtOf("test.StartTime",li_GetAsLocal,loo_Dt)
Write-Debug "month=" + string(loo_Dt.Month) + ", day=" + string(loo_Dt.Day) + ", year=" + string(loo_Dt.Year) + ", hour=" + string(loo_Dt.Hour) + ", minute=" + string(loo_Dt.Minute)

//  Output:

//  month=1, day=30, year=2018, hour=20, minute=35
//  month=4, day=24, year=2018, hour=13, minute=47
//  month=1, day=30, year=2018, hour=20, minute=35
//  month=4, day=24, year=2018, hour=13, minute=47
//  month=11, day=6, year=2015, hour=18, minute=36
//  month=9, day=21, year=2015, hour=23, minute=18
//  month=2, day=17, year=2018, hour=11, minute=37


destroy loo_Json
destroy loo_DateTime
destroy loo_Dt