0

I am just new to AJAX and JSON, JSON response etc many things are interleaved I don't know how these are working.

I just want to change the format of DATE in JSON response, I am getting response like this:

/Date(-62135596800000)/ 

I just want to change it into the HUMAN readable DATE format. HERE is the way how I am getting the values from database through DataTable using the columns from database and putting values into the view in c#.

 $('#myDbTable')
            .DataTable({
                ajax: {
                    url: "@Url.Action("GetVouchers", "Vouchers")",
                type: "GET",
                dataType: "json",
                dataSrc: 'DataSet',

            },
                "columns": [
                    { "data": "FinancialYearName" },
                    { "data": "CompanyName" },
                    { "data": "BusinessUnitName" },
                    { "data": "VoucherTypeName" },
                    { "data": "Prefix" },
                    { "data": "Code" },
                    { "data": "VoucherDate" },
                    { "data": "Status" }
                    
                ],
                "aoColumnDefs": [
                    {
                        "aTargets": [DataColumnsCount],
                        "mData": null,
                        "bSortable": false,
                        "mRender": function(data, type, fullRow) {
                            //                       console.log(fullRow);
                            
                            return '<a class="btn btn-info btn-sm" href=#/' + fullRow["Id"] + '>' + 'Edit' + '</a>';
                        }

                    }
                ]

            });

{ "data": "VoucherDate" } is the column which is showing the Date into the dataTable using the database column.

This is the image of OUTPUT for Date

I want to show it like 24 January, 2017.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Possible duplicate of [How do I format a Microsoft JSON date?](http://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date) – Izzy Jan 24 '17 at 08:42

2 Answers2

0

You can define a custom render function for your column where you could format the data coming from your server to a desired output:

{ 
    data: 'VoucherDate', 
    render: function (data, type, row) {
        var date = new Date(parseInt(data.replace("/Date(", "").replace(")/",""), 10));
        return date;
    }
}

Of course a much better solution would be to modify your server side serializer so that instead of splitting this format, it uses some more standard date format like ISO 8601 for example.

It seems that you are using ASP.NET MVC, so I would recommend you switching to theJson.NETserializer instead of relying on the JavaScriptSerializer class.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • i have edited my question now full code block is given into the snippet u please take a look and reply me . – Devil Mind Jan 24 '17 at 09:03
  • I have already shown in my answer how you can use the `render` function when defining the `VoucherDate` column. There's not much I can add to it. – Darin Dimitrov Jan 24 '17 at 10:10
  • i dont know how to use this in my code please do this for me :/ i don't know where to put render there is already mRender in the snippet – Devil Mind Jan 24 '17 at 10:16
  • You put it inside your `columns` definition. Currently you have this in your code: `{ "data": "VoucherDate" }`. What I am saying is to replace it with what I have shown in my answer. In addition to specifying the `data` field you specify a `render` function. – Darin Dimitrov Jan 24 '17 at 10:17
0

I am also working with MVC. You can format datetime in your c# methods.

Check this out : C# DateTime to "YYYYMMDDHHMMSS" format.

By the way, take care of timezones offset :)

Community
  • 1
  • 1
activ8
  • 181
  • 3
  • 17