uniDate — universal date sorting plugin

uniDate — universal date sorting plugin

ShevchukShevchuk Posts: 4Questions: 0Answers: 0
edited September 2011 in Plug-ins
https://github.com/shvchk/uniDate

Configurable, everything is optional, works for any all-numeric dates with non-numeric separators.
Minified version available: https://github.com/shvchk/uniDate/blob/master/uniDate.min.js — 543B (326B gzipped).
[code]
function uniDate(date) {
// split date by any non-digit character
var inputDate = date.split(/\D+/g);

/**
* Configure this to suit your needs!
*
* Your input date has its elements in particular order, and here
* you should describe it.
*
* E.g., if you have date like '2011.11', i.e. no days, no hours,
* nothing but year and month, you should set year's position to 1,
* month's position to 2, and all other's positions to whatever you
* like (I suggest 0 or 9).
*
* By default we assume your date to be in the following order:
* 'day.month.year hour:minute:second', but don't forget that any
* non-digit can be used as separator, so even 'day/month.year
* hour%minute::second' is ok :)
*
* So let's assume we have something like 24.12.2012 17:47:39.
*
* 24 . 12 .2012 17 : 47 : 39
* ^ ^ ^ ^ ^ ^
* day.month.year hour:minute:second
* ^ ^ ^ ^ ^ ^
* 1 2 3 4 5 6
*
* Here is how its description looks like:
*/
var universalDate = {
'year': 3,
'month': 2,
'day': 1,
'hour': 4,
'minute': 5,
'second': 6
};

/**
* That's it, no more configuration required.
*
* Don't change anything after this line,
* unless you know what you are doing.
*/

$.each(universalDate, function(key, value) {
if (inputDate[value - 1]) {
universalDate[key] = inputDate[value - 1].toString();
if (universalDate[key].length == 1) {
universalDate[key] = 0 + universalDate[key];
}
} else {
universalDate[key] = 0;
}
});

return (universalDate.year + universalDate.month + universalDate.day + universalDate.hour + universalDate.minute + universalDate.second) * 1;
}

jQuery.fn.dataTableExt.oSort['uniDate-asc'] = function(a, b) {
x = uniDate(a);
y = uniDate(b);

return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['uniDate-desc'] = function(a, b) {
x = uniDate(a);
y = uniDate(b);

return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited September 2011
    nice. would be better if you could pass a 2nd (optional) parameter to indicate order of fields to accomodate different date specifications, i.e. yyyy-mm-dd as opposed to assumming dd-mm-yyyy.

    i.e.
    uniDate("2012-12-24 17:47:39", [1, 2, 3, 4, 5, 6]);
    uniDate("21/1 12:00", [3, 2, 4, 5]); // assume current year, seconds = 00

    I'd use this.
  • ShevchukShevchuk Posts: 4Questions: 0Answers: 0
    Good point, fbas, thank you. I'll implement it when I have time (mb next week). Also, anyone is welcome to fork/modify it.
  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin
    Love it! Nice one Shevchuk. I'll add a link to it in the plug-in documentation for sorting shortly. I'm sure it will be hugely appreciated!

    Regards,
    Allan
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    also allowing strings in the 2nd param would make it easier to read

    i.e.
    uniDate("2012-12-24 17:47:39", ["year", "month", "day", "hours", "minutes", "seconds"]);
    uniDate("2012-12-24 17:47:39", ["yr", "mo", "day", "hour", "min", "sec"]); // abbrev
    uniDate("2012-12-24 17:47:39", ["y", "m", "d", "h", "i", "s"]); // php date format abbrevs

    mapping["year"] = mapping["yr"] = mapping["y"] = 1;
    mapping["month"] = mapping["mo"] = mapping["m"] = 2;
    //etc
  • totallyplandomtotallyplandom Posts: 27Questions: 1Answers: 1
    My apologies, but Datatables plugins still confuse me to no end. What portion of this code is the plugin, and what portion should we as the user edit? At what point in my main code do I call the function? Is it automatic? The distinction is not clear.
  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin
    These two examples will be of some use:

    http://datatables.net/release-datatables/examples/plug-ins/sorting_plugin.html
    http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html

    In this case, the code above is all for the sorting plug-in, so you need to include that (after you include the DataTables script, but before you initialise your table) and set the sType for the columns you want to sort in this manner (the second example), since there is no automatic type detection for this plug-in.

    Allan
  • ErikSErikS Posts: 16Questions: 0Answers: 0
    I like this a lot. Have not gotten it to work yet. Formatting by dates as dd/mm/yyyy hh:mm AM at the users request led me to this. I made a slight mod to try to handle the AM/PM issue:

    [code] // pull AM/PM value off the end. get the last 2 characters.
    var ampm = date.substring(date.length - 2, 2);
    // split date by any non-digit characters
    var inputDate = date.split(/\D+/g);
    if (ampm == "PM") {
    inputDate[3] = inputDate[3] + 12;
    }
    [/code]

    [code]
    "aoColumnDefs": [
    {"bSortable": false, "aTargets": [0,1,2,3,4]},
    {"sType": "uniDate", "aTargets": ["date"]},
    {"sType": "numeric", "aTargets": ["numeric"]}
    ],
    [/code]


    Still having no luck on the sorting. Actually, it sorts fine unless I have enough records to force an ajax call back to the server. Then the sorting is off. Might be going at this the wrong way.
  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin
    I think we are going to need a lot more detail than that I'm afraid. You are doing client-side sorting on only part of the data set? Are you using server-side processing or something else?

    Allan
  • ErikSErikS Posts: 16Questions: 0Answers: 0
    edited July 2012
    Allan, my main problem is that sorting on the date fields does not work because I am formatting them (user requirement in the form of mm/dd/yyyy hh:ii AM/PM. Found 2 issues with my code. My formatted dates are in a different order, and I do not pass seconds. Adjusting.

    Part 2: in order to get the date data to show correctly and formatted as desired, I was formatting in both the SQL and the html using jsp import="java.text.SimpleDateFormat". The order by on the SQL was generated using the formatted SQL column so the data ordered based upon the formatting. Revising.

    My date sorting is now going OK. But I am doing my sorting using mysql ORDER BY statements. (The ajax call calls the my sql where I change the WHERE and ORDER BY based upon the users selections. If the overall result set is < 250 rows, I set a flag to tell datatables not to use serverside when resorting. If > 250 rows, when sorting, I go back to the database.

    Are there examples of code for sorting server side? I mean the java code.
This discussion has been closed.