Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Node.js Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Node.js) JSON Date Parsing

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

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOSX, and ARM

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node11-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node11-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node11-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node11-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node11-linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('@chilkat/ck-node11-macosx');
}

function chilkatExample() {

    var success;

    var json = new chilkat.JsonObject();
    json.EmitCompact = false;

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

    console.log(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.
    var dateTime = new chilkat.CkDateTime();
    var dt = new chilkat.DtObj();
    var getAsLocal = false;

    // Load the date/time at test.timestamp into the dateTime object.
    success = json.DateOf("test.timestamp",dateTime);
    console.log(dateTime.GetAsTimestamp(getAsLocal));
    console.log(dateTime.GetAsUnixTime(false));
    console.log(dateTime.GetAsRfc822(getAsLocal));

    success = json.DateOf("test.rfc822",dateTime);
    console.log(dateTime.GetAsTimestamp(getAsLocal));

    json.I = 0;
    success = json.DateOf("test.dateStrings[i]",dateTime);
    console.log(dateTime.GetAsTimestamp(getAsLocal));

    json.I = 1;
    success = json.DateOf("test.dateStrings[i]",dateTime);
    console.log(dateTime.GetAsTimestamp(getAsLocal));

    success = json.DateOf("test.StartLoggingTime",dateTime);
    console.log(dateTime.GetAsTimestamp(getAsLocal));

    success = json.DateOf("test.Expiration",dateTime);
    console.log(dateTime.GetAsTimestamp(getAsLocal));

    success = json.DateOf("test.StartTime",dateTime);
    console.log(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("test.timestamp",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + dt.Minute);

    success = json.DtOf("test.rfc822",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + dt.Minute);

    json.I = 0;
    success = json.DtOf("test.dateStrings[i]",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + dt.Minute);

    json.I = 1;
    success = json.DtOf("test.dateStrings[i]",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + dt.Minute);

    success = json.DtOf("test.StartLoggingTime",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + dt.Minute);

    success = json.DtOf("test.Expiration",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + dt.Minute);

    success = json.DtOf("test.StartTime",getAsLocal,dt);
    console.log("month=" + dt.Month + ", day=" + dt.Day + ", year=" + dt.Year + ", hour=" + dt.Hour + ", minute="
         + 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

}

chilkatExample();

 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.