How to convert .NET Json Serialized Date

How to convert .NET Json Serialized Date

rwkiiirwkiii Posts: 13Questions: 5Answers: 0
edited July 2014 in Free community support

In my code below I am trying to convert a .NET Json serialized date to a human readable form, like maybe mm/dd/yyyy, but I'm not sure how to configure the DataTable column to call a function to make the conversion. All I would like to know is what the aoColumns configuration line would be in order to have the function ToJavaScriptDate() function called.

Can someone give me a pointer?

        var table = $('#jsonajaxtable').DataTable({
            dom: 'T<"clear">lfrtipS',
            "bProcessing": true,
            "iDisplayLength": 10,
            "sAjaxSource": "/Admin/UsersList",
            "sAjaxDataProp": "",
            "aoColumns": [
                {
                    "class": 'details-control',
                    "orderable": false,
                    "mDataProp": null,
                    "defaultContent": ''
                },
                { title: "Name", "mDataProp": "User.Name" },
                { title: "Company", "mDataProp": "User.Company" },
                { title: "Roles", "mDataProp": "RoleNames" },
---->>          { title: "Created Date", 
                         "mDataProp": function ("User.CreatedDate", type, full) { 
                          return ToJavaScriptDate(data); 
                          } 
                    }, 
               // { title: "Created Date", "mDataProp": "User.CreatedDate" },
                { title: "Last Login", "mDataProp": "User.LastLoginDate" }
            ],
            "order": [[2, 'asc']]
        });

Date conversion function:

    function ToJavaScriptDate(value) {
        var pattern = /Date\(([^)]+)\)/;
        var results = pattern.exec(value);
        var dt = new Date(parseFloat(results[1]));
        return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
    }

Answers

  • rwkiiirwkiii Posts: 13Questions: 5Answers: 0
    edited July 2014

    I tried for several hours to make this work last night. After posting my question on how to do this I gave it another try and it appears to be working. Here is the changed line:

        { title: "Created Date", 
                "mDataProp": "User.CreatedDate", 
                "render": function (data, type, row) { 
                return ToJavaScriptDate(data);
                }
        },
    

    If this isn't 100% I would appreciate any pointers. It seems to render the date fine this way. ;)

This discussion has been closed.