0

I'm not even sure how to ask this question. I've encountered a very strange issue involving a DataTables.net control on an aspx page with the control being populated using an ajax call to retrieve the results.

I had been returning dates as strings but when clicking the column header the sort was all wrong. So I changed the return results to use DateTime? type for dates instead of string. That solved the sorting problem but now the date was formatted badly. So I found this post: format json date to mm/dd/yy format before displaying in a jquery datatable I used this example:

$('#userData').DataTable({
    columns: [
        { "data": "userId"},
        { "data": "userCreated",
          "type": "date ",
          "render":function (value) {
              if (value === null) return "";

              var pattern = /Date\(([^)]+)\)/;
              var results = pattern.exec(value);
              var dt = new Date(parseFloat(results[1]));

              return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear(); }
        }
    ]
};

However, when I ran the code all my dates came out in the year 1929. So I checked to see what the dates looked like in the resulting json data returned to the ajax call and the date data looks like this:

2017-10-29T19:57:46

So I modified my code to this:

{
    data: 'CreatedDate',
    "orderable": true,
    "searchable": true,
    "title": "Created Date",
    "type": "date",
    "render": function(value) {
        if (value === "") {
            return value;
        }
        var dt = new Date(value);
        return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
    }
},

That worked so I committed to GIT and did a build and deployed to QA. The code is working fine in that environment.

Next, I branched from the branch I just delivered to QA so I could start work on the next issue which has nothing to do with dates. I make some minor changes that did not impact the code that returns the results for the control. I test. Mysteriously all the dates are displaying in the year 1929 again. I check the json returned to the ajax call and now the date data looks like this:

"/Date(1510112492000)/"

So I revert my code back to the original example and now the dates are showing correctly in the control.

My question is, what caused the json date data to change form? What can I expect to happen when I deploy to the QA environment? I'm so confused.

2
  • I don't know if this will matter, but in your original code (the borrowed bit), there's a parenthesis missing at the end. Commented Mar 9, 2018 at 3:33
  • Thanks. I ended up committing the code with the original example and all is working correctly. It is still puzzling to me but is working so I'm not going to spend any more cycles on it. Commented Mar 9, 2018 at 14:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.