0

i have the following jquery datatable column 'DOB' associated with a function

columns:[
{
    'data': 'DOB',
    'visible': false,
    'render': function (jsonDate) {
        var date = new Date(parseInt(jsonDate.substr(6)));
        var month = date.getMonth() + 1;
        return month + '/' + date.getDate() + '/' + date.getFullYear();
     }
},
]

but when i retrieve the value it retrieves as Json date /Date(-133335000000)/ why this happens and how do i fix this?

Phill Greggan
  • 2,234
  • 3
  • 20
  • 33
  • What you are getting as `jsonDate`? – Guruprasad J Rao Jan 14 '16 at 04:02
  • Not sure I understand the question. Are you saying your code (the `'render': function (jsonDate) {` function) is not working, or are you wanting to understand why the returned value is in the format `/Date(-133335000000)/` (instead of `03/24/1974`)? –  Jan 14 '16 at 04:21
  • @StephenMuecke why the return value in json date format not in the mm/dd/yyyy format? – Phill Greggan Jan 14 '16 at 04:23
  • I think the answers [here](http://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format) give a good explanation. Personally, I would consider returning the value of the `DateTime` property as a formatted `string` in the controller method rather than adding this code –  Jan 14 '16 at 04:26
  • @StephenMuecke if im to do that, i need to modify the model class DOB type to string from DateTime... that would effect other code related to it – Phill Greggan Jan 14 '16 at 04:31
  • 1
    No, you can just return a collection of anonymous objects (which you probably should be doing anyway unless you displaying every property of the model) - `var data = db.someTable.ToList().Select(x => new { ID = x.ID, DOB = x.DOB.ToString("d"), etc ...}; return Json(data, ...);` –  Jan 14 '16 at 04:34
  • @StephenMuecke im not returning an anonymous collection im returning of type EmployeeDetail so in it has DOB as datetime – Phill Greggan Jan 14 '16 at 04:36
  • 1
    Yes, but you can return an anonymous object as per the code in my last comment (excepy it will be `db.EmployeeDetails.ToList()....`. That is the way I would handle it (if nothing else but to ensure only the properties required in the view are sent to the view), but there is nothing wrong with what your currently doing –  Jan 14 '16 at 04:40

0 Answers0