Date sorting based on site or locale
Date sorting based on site or locale
dylanmac
Posts: 49Questions: 7Answers: 1
We're using datatables in a platform project where we have multiple sites in multiple locales using the same code but different data. Some date column data will be in US format, e.g. mm/dd/yyyy whereas others may use dd-mm-yyyy. Is there a way to set the format that datatables uses to sort dates on a locale or site basis?
If there isn't, I'm thinking we would need to include date format plugins for each date format we want to support and invoke those via the columnDefs.type/columnDefs.target attributes keyed off of a page level attribute, e.g. . For example:
[code]
var columnDefs = null;
if ($('#uk').length) {
columnDefs = [
{
"type": 'date-eu-pre',
"targets": ['dateField1','dateField2','dateField3']
}
]
} else if ($('#us').length) {
columnDefs = [
{
"type": 'date-pre',
"targets": ['dateField1','dateField2','dateField3']
}
]
}
$(document).ready(function() {
$('#example').dataTable( {
"columnDefs": columnDefs
} );
} );
[/code]
If there isn't, I'm thinking we would need to include date format plugins for each date format we want to support and invoke those via the columnDefs.type/columnDefs.target attributes keyed off of a page level attribute, e.g. . For example:
[code]
var columnDefs = null;
if ($('#uk').length) {
columnDefs = [
{
"type": 'date-eu-pre',
"targets": ['dateField1','dateField2','dateField3']
}
]
} else if ($('#us').length) {
columnDefs = [
{
"type": 'date-pre',
"targets": ['dateField1','dateField2','dateField3']
}
]
}
$(document).ready(function() {
$('#example').dataTable( {
"columnDefs": columnDefs
} );
} );
[/code]
This discussion has been closed.
Replies
DataTables uses `Date.parse()` to determine date types, using the built in options. The only reliable form that supports across all browsers is ISO8601. The ECMAScript specification doesn't say what formats browsers should support, so every browser is different...
The best way of doing it, imho, is using orthogonal data, rather like what robertbrower is suggesting (which is one possible method - although note that fnRender is removed in 1.10!).
There is a manual page on the topic here: http://next.datatables.net/manual/orthogonal-data
Allan