Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oDateTime
LOCAL oDt
LOCAL nGetAsLocal
nSuccess := 0
oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0
// First, let's create JSON containing some date/time strings.
oJson:UpdateString("test.timestamp", "2018-01-30T20:35:00Z")
oJson:UpdateString("test.rfc822", "Tue, 24 Apr 2018 08:47:03 -0500")
oJson:UpdateString("test.dateStrings[0]", "2018-01-30T20:35:00Z")
oJson:UpdateString("test.dateStrings[1]", "Tue, 24 Apr 2018 08:47:03 -0500")
oJson:UpdateNumber("test.StartLoggingTime", "1446834998.695")
oJson:UpdateNumber("test.Expiration", "1442877512.0")
oJson:UpdateInt("test.StartTime", 1518867432)
? oJson: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.
oDateTime := CreateObject("Chilkat.CkDateTime")
oDt := CreateObject("Chilkat.DtObj")
nGetAsLocal := 0
// Load the date/time at test.timestamp into the dateTime object.
nSuccess := oJson:DateOf("test.timestamp", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
? Str(oDateTime:GetAsUnixTime(0))
? oDateTime:GetAsRfc822(nGetAsLocal)
nSuccess := oJson:DateOf("test.rfc822", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
oJson:I := 0
nSuccess := oJson:DateOf("test.dateStrings[i]", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
oJson:I := 1
nSuccess := oJson:DateOf("test.dateStrings[i]", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
nSuccess := oJson:DateOf("test.StartLoggingTime", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
nSuccess := oJson:DateOf("test.Expiration", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
nSuccess := oJson:DateOf("test.StartTime", oDateTime)
? oDateTime:GetAsTimestamp(nGetAsLocal)
// 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:
nSuccess := oJson:DtOf("test.timestamp", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt:Minute)
nSuccess := oJson:DtOf("test.rfc822", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt:Minute)
oJson:I := 0
nSuccess := oJson:DtOf("test.dateStrings[i]", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt:Minute)
oJson:I := 1
nSuccess := oJson:DtOf("test.dateStrings[i]", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt:Minute)
nSuccess := oJson:DtOf("test.StartLoggingTime", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt:Minute)
nSuccess := oJson:DtOf("test.Expiration", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt:Minute)
nSuccess := oJson:DtOf("test.StartTime", nGetAsLocal, oDt)
? "month=" + Str(oDt:Month) + ", day=" + Str(oDt:Day) + ", year=" + Str(oDt:Year) + ", hour=" + Str(oDt:Hour) + ", minute=" + Str(oDt: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
oJson:destroy()
oDateTime:destroy()
oDt:destroy()