From kodeboard, 4 Years ago, written in JavaScript.
Embed
  1. //initialize on page load
  2. $(document).ready(function(){
  3.  
  4.         //Function to format date
  5.     var customDateDDMMMYYYYToOrd = function (date) {
  6.         // Convert to a number YYYYMMDD which we can use to order
  7.         var dateParts = date.split(' ');
  8.         var m = dateParts[0];
  9.         var d = dateParts[1];
  10.         var y = dateParts[2];
  11.  
  12.         m = ($.inArray(m.toUpperCase(), ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"])) + 1;
  13.         if(m < 10) {
  14.             m = '0' + m;
  15.         }
  16.  
  17.         d = d.replace(/[^\d]+/,'');    
  18.  
  19.         var full = (y + m + d);
  20.         return full;
  21.     };
  22.  
  23.     // define the sorting datatables column algorithm
  24.     jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-desc'] = function (a, b) {
  25.         var ordA = customDateDDMMMYYYYToOrd(a),
  26.             ordB = customDateDDMMMYYYYToOrd(b);
  27.         return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
  28.     };
  29.  
  30.   //work on a table : <table id="reservations">
  31.     $('#reservations').dataTable({
  32.         //Sort by 4th column (the timeago column)
  33.         "aaSorting": [[ 3, "desc" ]],
  34.        
  35.        
  36.         "aoColumnDefs" : [
  37.             {
  38.                 //sort the date - returned from db as "May 29th 2013"  - this is for the first column
  39.                 "sType": "date-dd-mmm-yyyy",
  40.                 "aTargets" : [0],
  41.             },
  42.             {
  43.               //render the date column from mysql datetime to timeago
  44.               //data is the raw yyyy-mm-dd h:i:s
  45.                 "mRender" : function(data, type, row) {
  46.                     if('display' === type) {
  47.                         return $.timeago(data);
  48.                     }
  49.                     return data;
  50.                 },
  51.                 "aTargets" : [3],
  52.             }
  53.  
  54.         ]
  55.     });
  56.  
  57. });

You must login to reply or comment on this code.