I am using the datatables jquery plugin and want to sorty by dates.
I know they got a plugin but I can\'t find where to actually download it from
http://data
I Have 10 Columns in my table and there is 2 columns of dates, 2nd column and 4th column is of US Date, so this is worked for me fine. Just paste this code at last in your script section in same sequence.
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function (a, b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function (a, b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$('#tblPoSetupGrid').dataTable({
columnDefs: [
{ type: 'us_date', targets: 3 },
{ type: 'us_date', targets: 1 }
]
});
I got solution after working whole day on it. It is little hacky solution Added span inside td tag
<td><span><%= item.StartICDate %></span></td>.
Date format which Im using is dd/MM/YYYY. Tested in Datatables1.9.0
Convert the date to the format YYYYMMDD and prepend to the actual value (MM/DD/YYYY) in the <td>
, wrap it in an element, set style display:none;
to the elements. Now the date sort will work as a normal sort. The same can be applied to date-time sort.
HTML
<table id="data-table">
<tr>
<td><span>YYYYMMDD</span>MM/DD/YYYY</td>
</tr>
</table>
CSS
#data-table span {
display:none;
}
As of 2015, the most convenient way according to me to sort Date column in a DataTable, is using the required sort plugin. Since the date format in my case was dd/mm/yyyy hh:mm:ss
, I ended up using the date-euro plugin.
All one needs to do is:
Step 1: Include the sorting plugin JavaScript file or code and;
Step 2: Add columnDefs
as shown below to target appropriate column(s).
$('#example').dataTable( {
columnDefs: [
{ type: 'date-euro', targets: 0 }
]
});
Click on the "show details" link under Date (dd/mm/YYY), then you can copy and paste that plugin code provided there
Update: I think you can just switch the order of the array, like so:
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function(a,b) {
var usDatea = a.split('/');
var usDateb = b.split('/');
var x = (usDatea[2] + usDatea[0] + usDatea[1]) * 1;
var y = (usDateb[2] + usDateb[0] + usDateb[1]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function(a,b) {
var usDatea = a.split('/');
var usDateb = b.split('/');
var x = (usDatea[2] + usDatea[0] + usDatea[1]) * 1;
var y = (usDateb[2] + usDateb[0] + usDateb[1]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
All I did was switch the __date_[1]
(day) and __date_[0]
(month), and replaced uk
with us
so you won't get confused. I think that should take care of it for you.
Update #2: You should be able to just use the date object for comparison. Try this:
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function(a,b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function(a,b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
Specify the column's type
as type: 'date'
:
{title: 'Expiration Date', data: 'ExpirationDate', type: 'date'}