0

I am using datatables v1.10 and need to format date to dd/mm/yyyy h:i:s format. I have tried various suggestions that I have foumd on SO and google but nothing sems to work My latest effort is attached and just changes all dates to: 7-2-2011. My date format that is being returned is: intkdate 2011-03-07 16:12:39. I am confused as to why it is changing the date and would be grateful if someone could point out my error.

I know this question will get downvoted but I cannot find a better alternative for a title. Many thanks

"columns": [
      { mData: 'id' } ,
      { mData: 'box', 'width': '40' },
      { mData: 'intkdate'},
      { mData: 'destdate' },
      { "mData": null, 'width': '110' }
      ],
"columnDefs": [ 
      {
     targets: 2,
        "render": function (data) {
        var date = new Date(data);
        return  date.getDate() + "-"+date.getMonth()+"-" + date.getFullYear();
    }
      },
user1532468
  • 1,723
  • 8
  • 41
  • 80
  • What is your input **data** in **new Date(data)** ? – Alpesh Jikadra Apr 01 '18 at 07:10
  • Put an actual `Date` object in your data and let a date library (like moment.js) take care of formatting it for display. Don't write your own date formatting code, don't store dates as strings. – Tomalak Apr 01 '18 at 07:21
  • @AlpeshJikadra I am assuming it would be `targets: 2`. I have even tried `var date = new Date(data.intkdate);` but tis just produces `NaN`. Tanks – user1532468 Apr 01 '18 at 07:21
  • @Tomalak I have moment loaded but couldn't get it to work. Could you do a snippet based on your suggestion. Thanks – user1532468 Apr 01 '18 at 07:22
  • @Tomalak where is the link to the duplicate question> thanks – user1532468 Apr 01 '18 at 07:24
  • @user1532468 I'm sorry, no. "I couldn't get it to work" is not good enough when I a) just linked to a question with working code and b) see none of your attempts. Using moment.js isn't rocket science. – Tomalak Apr 01 '18 at 07:25
  • 1
    @Tomalak Thanks for link. Got it working. How to i handle `0000-00-00` which show as invalid date. Thanks – user1532468 Apr 01 '18 at 07:35
  • Well, `0000-00-00` is an invalid date, isn't it? :) But good to hear you got it working, and that it wasn't so hard after all. – Tomalak Apr 01 '18 at 07:44

0 Answers0