jQuery DataTables plugin: Sort German date

I use jQuery DataTables plugin and my problem is that my German date does not get ordered correctly. It has the following format: dd.mm.YYYY HH:iih

Here comes my code:

JSFIDDLE:

https://jsfiddle.net/uxaLn1e3/3/

HTML:

    <table id="my-table">
   <thead>
      <th>Nr. </th>
      <th>Date</th>
      <th>Name</th>
   </thead>
   <tr>
      <td>1</td>
      <td>27.08.2015 19:00h</td>
      <td>Carl</td>
   </tr>
   <tr>
      <td>2</td>
      <td>10.02.2016 14:00h</td>
      <td>Alan</td>
   </tr>
   <tr>
      <td>3</td>
      <td>07.12.2015 21:00h</td>
      <td>Bobby</td>
   </tr>
</table>

JS (updated, with ajax):

     $('#my-table').DataTable({
            "ajax": 'my_url',
            "columns": [
                {"data": "nr"},
                {"data": "date"},
                {"data": "name"}
            ],
                "autoWidth": false,
                "order": [],
                "fnCreatedRow": function( nRow, aData, iDataIndex ) {
                   var dateFull = aData.date;
                   var dateFullItems = dateFull.split(' ');
                   var dateDatum = dateFullItems[0];
                   var dateDatumItems = dateDatum.split('.');
                  var dateTime = dateFullItems[1];
                  var dateFormat = dateDatumItems[2] + '-' + dateDatumItems[1] + '-' + dateDatumItems[0] + 'T' + dateTime + ':00Z'; 

                  $(nRow).find('td:nth-of-type(2)').attr('data-sort', dateFormat);
            },
            });

What adjustments do I need to do in my JS for the sorting of date to work?

Answers 1

Related Articles