Date sorting based on site or locale

Date sorting based on site or locale

dylanmacdylanmac Posts: 49Questions: 7Answers: 1
edited March 2014 in DataTables 1.10
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]

Replies

  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    Dates and internationalization are a pain in the yingyang. One idea is to put the number of milliseconds since the UNIX Epoc in the Date column instead of the text representation of the date. Then you can use fnRender to create a new Date(millisecondsSinceEpoc) and call it's methods to return whatever text representation you want.
  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    > Is there a way to set the format that datatables uses to sort dates on a locale or site basis?

    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
This discussion has been closed.