Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Parse a Microsoft JSON Date (MS AJAX Date)

See more JSON Examples

Demonstrates how to parse a Microsoft JSON Date, also known as an MSAJAX date.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Parse Microsoft JSON Dates (AJAX Dates)
    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    try json.load("{ \"AchievementDate\":\"/Date(1540229468330-0500)/\"}");

    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    json.dateOf("AchievementDate", dt) catch {
        std.debug.print("Unable to parse a date/time.\n", .{});
        return;
    };

    // Show the date in different formats:
    const b_local = true;
    std.debug.print("RFC822: {s}\n", .{try dt.getAsRfc822(alloc, b_local)});
    std.debug.print("Timestamp: {s}\n", .{try dt.getAsTimestamp(alloc, b_local)});
    std.debug.print("YYYY-MM-DD: {s}\n", .{try dt.getAsIso8601(alloc, "YYYY-MM-DD", b_local)});

    // Get integer values for year, month, day, etc.
    const dt_obj = try chilkat.DtObj.init();
    defer dt_obj.deinit();
    dt.toDtObj(b_local, dt_obj);

    std.debug.print("year: {d}\n", .{dt_obj.getYear()});
    std.debug.print("month: {d}\n", .{dt_obj.getMonth()});
    std.debug.print("day: {d}\n", .{dt_obj.getDay()});
    std.debug.print("hour: {d}\n", .{dt_obj.getHour()});
    std.debug.print("minute: {d}\n", .{dt_obj.getMinute()});
    std.debug.print("seconds: {d}\n", .{dt_obj.getSecond()});

    // Sample output:
    // RFC822: Mon, 22 Oct 2018 17:31:08 -0500
    // Timestamp: 2018-10-22T17:31:08-05:00
    // YYYY-MM-DD: 2018-10-22
    // year: 2018
    // month: 10
    // day: 22
    // hour: 17
    // minute: 31
    // seconds: 8
}