Where can I convert JSON date format when data from WCF ajax call?

I am getting JSON data from WCF service like below format. Then I am binding that data to handsOnTables.

JSON
{"d":[
  { "__type":"InitData:#company.ReductionData",
    "Owner":"test9",
    ...
    ... 
    "StartDate":"\/Date(1366848000000+0000)\/",
    "Risks":"test9",
 }] }
JSCript
$(function () {

         var $value = $("#Body_hiddenClientId");
        // alert($value.val());
         var $container = $("#dataTable");

         $container.handsontable({
             contextMenu: true,
             startRows: 1,
             minRows: 1,                 
             colHeaders: true,
             stretchH: 'all',     
             columns: [
                 { data: 'Owner' },
                 ...
                 ....
                 { data: 'StartDate', type: 'date', dateFormat: 'mm/dd/yy' },
                 { data: 'Risk' }                   
               ]
         });

         var handsontable = $container.data('handsontable');
         $.ajax({
             url: "/EditInitiatives.svc/GetGridData",
             data: "session=" + $value.val(),
             type: "GET",
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (res) {                   
                 $container.handsontable('loadData', res.d);    
             },
             error: function (error) {
                 alert("Error: " + error.responseText);
             }
         });
     });

I don't know How to convert into 'mm/dd/yy' format? Where to convert format in the code?

Answers


handsontable expects a string to format date. you should convert it to the proper form.

try something like this:

change this:

...
success: function (res) {                  
    $container.handsontable('loadData', res.d);    
},
...

with this:

...
success: function (res) {
    for(var i in res.d) {
        // get the milliseconds from your string "StartDate":"\/Date(1366848000000+0000)\/",
        // and create new Date object
        var date = new Date(+(res.d[i]['StartDate'].match(/\d+/i))); 
        // then put it back with format mm/dd/yy            
        res.d[i]['StartDate'] = (date.getMonth() + 1) + '-' + date.getDate() + date.getFullYear();
    }               
    $container.handsontable('loadData', res.d);    
},
...

You can also do the conversion server side to format it as the desired string. Here is some code I've used with my WCF service using C#:

        var adList = someSource
            .Select(ad => new
            {
                WhenTransacted = ad.WhenTransacted.HasValue ? ad.WhenTransacted.Value.ToString("MM/dd/yy") : string.Empty,
                Amount = ad.Amount,
                Reference = ad.Reference,
            })
            .ToList();
        return JsonConvert.SerializeObject(adList);

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.