Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>
#include <CkDtObjW.h>
void ChilkatSample(void)
{
bool success = false;
CkJsonObjectW json;
json.put_EmitCompact(false);
// First, let's create JSON containing some date/time strings.
json.UpdateString(L"test.timestamp",L"2018-01-30T20:35:00Z");
json.UpdateString(L"test.rfc822",L"Tue, 24 Apr 2018 08:47:03 -0500");
json.UpdateString(L"test.dateStrings[0]",L"2018-01-30T20:35:00Z");
json.UpdateString(L"test.dateStrings[1]",L"Tue, 24 Apr 2018 08:47:03 -0500");
json.UpdateNumber(L"test.StartLoggingTime",L"1446834998.695");
json.UpdateNumber(L"test.Expiration",L"1442877512.0");
json.UpdateInt(L"test.StartTime",1518867432);
wprintf(L"%s\n",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.
CkDateTimeW dateTime;
CkDtObjW dt;
bool getAsLocal = false;
// Load the date/time at test.timestamp into the dateTime object.
success = json.DateOf(L"test.timestamp",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(getAsLocal));
wprintf(L"%u\n",dateTime.GetAsUnixTime(false));
wprintf(L"%s\n",dateTime.getAsRfc822(getAsLocal));
success = json.DateOf(L"test.rfc822",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(getAsLocal));
json.put_I(0);
success = json.DateOf(L"test.dateStrings[i]",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(getAsLocal));
json.put_I(1);
success = json.DateOf(L"test.dateStrings[i]",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(getAsLocal));
success = json.DateOf(L"test.StartLoggingTime",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(getAsLocal));
success = json.DateOf(L"test.Expiration",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(getAsLocal));
success = json.DateOf(L"test.StartTime",dateTime);
wprintf(L"%s\n",dateTime.getAsTimestamp(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:
success = json.DtOf(L"test.timestamp",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_Minute());
success = json.DtOf(L"test.rfc822",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_Minute());
json.put_I(0);
success = json.DtOf(L"test.dateStrings[i]",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_Minute());
json.put_I(1);
success = json.DtOf(L"test.dateStrings[i]",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_Minute());
success = json.DtOf(L"test.StartLoggingTime",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_Minute());
success = json.DtOf(L"test.Expiration",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_Minute());
success = json.DtOf(L"test.StartTime",getAsLocal,dt);
wprintf(L"month=%d, day=%d, year=%d, hour=%d, minute=%d\n",dt.get_Month(),dt.get_Day(),dt.get_Year(),dt.get_Hour(),dt.get_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
}