Sorting by Date (dd/mm/YYYY hh:ii:ss) (Brazilian format)

Sorting by Date (dd/mm/YYYY hh:ii:ss) (Brazilian format)

rodolphopivettarodolphopivetta Posts: 1Questions: 0Answers: 0
edited March 2015 in Free community support

Hi there,

I'm brazilian and needed to sort a table by the dd/mm/YYYY hh:ii:ss format and don't find any solution. For this reason I decided implement that.

This solution was based in the Date (dd-mmm-yyyy) format.

So, there goes a solution to this format.

    (function () {
            var customDateDDMMYYYY_hhiiss = function (date) {
                "use strict"; //let's avoid tom-foolery in this function
                var date_parts = date.split(/\//);
                var hours = date_parts[2].split(/ /);
                date_parts[2] = hours[0];
                var hours_parts = hours[1].split(/:/);
                return (date_parts[2] + date_parts[1] + date_parts[0] +
                        hours_parts[0] + hours_parts[1] + hours_parts[2]);
            }

            jQuery.fn.dataTableExt.aTypes.unshift(
                function (sData) {
                    "use strict"; //let's avoid tom-foolery in this function
                    if (/^([0-2]?\d|3[0-1])\/(0[1-9]|1[012])\/\d{4} (0[0-9]|[0-2][0-3]):([0-5][0-9]):([0-5][0-9])$/i.test(sData)) {
                        return 'date-dd/mm/YYYY_hh:ii:ss';
                    }
                    return null;
                }
            );

            jQuery.fn.dataTableExt.oSort['date-dd/mm/YYYY_hh:ii:ss-asc'] = function (a, b) {
                "use strict"; //let's avoid tom-foolery in this function
                var ordA = customDateDDMMYYYY_hhiiss(a),
                    ordB = customDateDDMMYYYY_hhiiss(b);
                return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
            };

            jQuery.fn.dataTableExt.oSort['date-dd/mm/YYYY_hh:ii:ss-desc'] = function (a, b) {
                "use strict"; //let's avoid tom-foolery in this function
                var ordA = customDateDDMMYYYY_hhiiss(a),
                    ordB = customDateDDMMYYYY_hhiiss(b);
                return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
            };
        })();

if this code has some bug, please report and we will can try solve that.

Thanks

Replies

This discussion has been closed.