Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) JSON Date ParsingDemonstrates how to parse date/time strings from JSON. Note: This example uses the DtOf and DateOf methods introduced in Chilkat v9.5.0.73
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoJson Variant vDateTime Handle hoDateTime Variant vDt Handle hoDt Boolean iGetAsLocal String sTemp1 Integer iTemp1 Integer iTemp2 Integer iTemp3 Integer iTemp4 Integer iTemp5 Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End Set ComEmitCompact Of hoJson To False // First, let's create JSON containing some date/time strings. Get ComUpdateString Of hoJson "test.timestamp" "2018-01-30T20:35:00Z" To iSuccess Get ComUpdateString Of hoJson "test.rfc822" "Tue, 24 Apr 2018 08:47:03 -0500" To iSuccess Get ComUpdateString Of hoJson "test.dateStrings[0]" "2018-01-30T20:35:00Z" To iSuccess Get ComUpdateString Of hoJson "test.dateStrings[1]" "Tue, 24 Apr 2018 08:47:03 -0500" To iSuccess Get ComUpdateNumber Of hoJson "test.StartLoggingTime" "1446834998.695" To iSuccess Get ComUpdateNumber Of hoJson "test.Expiration" "1442877512.0" To iSuccess Get ComUpdateInt Of hoJson "test.StartTime" 1518867432 To iSuccess Get ComEmit Of hoJson To sTemp1 Showln sTemp1 // 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. Get Create (RefClass(cComCkDateTime)) To hoDateTime If (Not(IsComObjectCreated(hoDateTime))) Begin Send CreateComObject of hoDateTime End Get Create (RefClass(cComChilkatDtObj)) To hoDt If (Not(IsComObjectCreated(hoDt))) Begin Send CreateComObject of hoDt End Move False To iGetAsLocal // Load the date/time at test.timestamp into the dateTime object. Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.timestamp" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Get ComGetAsUnixTime Of hoDateTime False To iTemp1 Showln iTemp1 Get ComGetAsRfc822 Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.rfc822" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Set ComI Of hoJson To 0 Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.dateStrings[i]" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Set ComI Of hoJson To 1 Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.dateStrings[i]" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.StartLoggingTime" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.Expiration" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 Get pvComObject of hoDateTime to vDateTime Get ComDateOf Of hoJson "test.StartTime" vDateTime To iSuccess Get ComGetAsTimestamp Of hoDateTime iGetAsLocal To sTemp1 Showln sTemp1 // 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: Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.timestamp" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.rfc822" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 Set ComI Of hoJson To 0 Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.dateStrings[i]" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 Set ComI Of hoJson To 1 Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.dateStrings[i]" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.StartLoggingTime" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.Expiration" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 Get pvComObject of hoDt to vDt Get ComDtOf Of hoJson "test.StartTime" iGetAsLocal vDt To iSuccess Get ComMonth Of hoDt To iTemp1 Get ComDay Of hoDt To iTemp2 Get ComYear Of hoDt To iTemp3 Get ComHour Of hoDt To iTemp4 Get ComMinute Of hoDt To iTemp5 Showln "month=" iTemp1 ", day=" iTemp2 ", year=" iTemp3 ", hour=" iTemp4 ", minute=" iTemp5 // 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 End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.