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
(Swift 3,4,5...) 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
func chilkatTest() { var success: Bool let json = CkoJsonObject()! json.emitCompact = false // First, let's create JSON containing some date/time strings. json.update("test.timestamp", value: "2018-01-30T20:35:00Z") json.update("test.rfc822", value: "Tue, 24 Apr 2018 08:47:03 -0500") json.update("test.dateStrings[0]", value: "2018-01-30T20:35:00Z") json.update("test.dateStrings[1]", value: "Tue, 24 Apr 2018 08:47:03 -0500") json.updateNumber("test.StartLoggingTime", numericStr: "1446834998.695") json.updateNumber("test.Expiration", numericStr: "1442877512.0") json.updateInt("test.StartTime", value: 1518867432) print("\(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. let dateTime = CkoDateTime()! let dt = CkoDtObj()! var getAsLocal: Bool = false // Load the date/time at test.timestamp into the dateTime object. success = json.date(of: "test.timestamp", dateTime: dateTime) print("\(dateTime.getAsTimestamp(getAsLocal)!)") print("\(dateTime.getAsUnixTime(false))") print("\(dateTime.getAsRfc822(getAsLocal)!)") success = json.date(of: "test.rfc822", dateTime: dateTime) print("\(dateTime.getAsTimestamp(getAsLocal)!)") json.i = 0 success = json.date(of: "test.dateStrings[i]", dateTime: dateTime) print("\(dateTime.getAsTimestamp(getAsLocal)!)") json.i = 1 success = json.date(of: "test.dateStrings[i]", dateTime: dateTime) print("\(dateTime.getAsTimestamp(getAsLocal)!)") success = json.date(of: "test.StartLoggingTime", dateTime: dateTime) print("\(dateTime.getAsTimestamp(getAsLocal)!)") success = json.date(of: "test.Expiration", dateTime: dateTime) print("\(dateTime.getAsTimestamp(getAsLocal)!)") success = json.date(of: "test.StartTime", dateTime: dateTime) print("\(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.dt(of: "test.timestamp", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") success = json.dt(of: "test.rfc822", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") json.i = 0 success = json.dt(of: "test.dateStrings[i]", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") json.i = 1 success = json.dt(of: "test.dateStrings[i]", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") success = json.dt(of: "test.StartLoggingTime", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") success = json.dt(of: "test.Expiration", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") success = json.dt(of: "test.StartTime", bLocal: getAsLocal, dt: dt) print("month=\(dt.month.intValue), day=\(dt.day.intValue), year=\(dt.year.intValue), hour=\(dt.hour.intValue), minute=\(dt.minute.intValue)") // 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 } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.