Ultimate Date / Time sorting

Sort date and time in any format using Moment.js

This plug-in for DataTables represents the ultimate option in extensibility for sorting date / time strings correctly. It uses Moment.js to create automatic type detection and sorting plug-ins for DataTables based on a given format. This way, DataTables will automatically detect your temporal information and sort it correctly.

For usage instructions, please see the DataTables blog post that introduces it.

Plug-in code

(function (factory) {
    if (typeof define === "function" && define.amd) {
        define(["jquery", "moment", "datatables.net"], factory);
    } else {
        factory(jQuery, moment);
    }
}(function ($, moment) {

$.fn.dataTable.moment = function ( format, locale ) {
    var types = $.fn.dataTable.ext.type;

    // Add type detection
    types.detect.unshift( function ( d ) {
        if ( d ) {
            // Strip HTML tags and newline characters if possible
            if ( d.replace ) {
                d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
            }

            // Strip out surrounding white space
            d = $.trim( d );
        }

        // Null and empty values are acceptable
        if ( d === '' || d === null ) {
            return 'moment-'+format;
        }

        return moment( d, format, locale, true ).isValid() ?
            'moment-'+format :
            null;
    } );

    // Add sorting method - use an integer for the sorting
    types.order[ 'moment-'+format+'-pre' ] = function ( d ) {
        if ( d ) {
            // Strip HTML tags and newline characters if possible
            if ( d.replace ) {
                d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
            }

            // Strip out surrounding white space
            d = $.trim( d );
        }
        
        return !moment(d, format, locale, true).isValid() ?
            Infinity :
            parseInt( moment( d, format, locale, true ).format( 'x' ), 10 );
    };
};

}));

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$.fn.dataTable.moment( 'HH:mm MMM D, YY' );
  $.fn.dataTable.moment( 'dddd, MMMM Do, YYYY' );

  $('#example').DataTable();