Quantcast
Channel: Free community support — DataTables forums
Viewing all articles
Browse latest Browse all 35454

How to convert .NET Json Serialized Date

$
0
0

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();
    }

Viewing all articles
Browse latest Browse all 35454