Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to shim the "dd-MON-yyyy" date format for IE

The organization I support dictates that all dates will be shown in dd-MON-yyyy format (10-SEP-2006, 08-MAY-2013). Please see http://jsfiddle.net/jhfrench/zpWWa/ for an example dataset.

When run on Chrome, dataTables correctly recognizes this pattern as a date.

When run on IE7, dataTables (or IE?) does not recognize this pattern as a date. Unfortunately, we have to support IE7. Is there a way to shim the "dd-MON-yyyy" format for IE, but not for Chrome or other browsers that natively support that format?

I am using IE conditions to specify the HTML tag, so I could key off of <HTML class="lt-ie9">; I'm also using Modernizr on this page (if there's a relevant test).

like image 520
Jeromy French Avatar asked May 08 '13 20:05

Jeromy French


People also ask

What is the dd-mmm-yyyy format?

The dd-mmm-yyyy format is specified in RFC 5322 "Internet Message Format": 3.3. Date and Time Specification Date and time values occur in several header fields. This section specifies the syntax for a full date and time specification.

How to make sure the date format in a variable is 'DD-Mon-YYYY'?

user11090588 wrote: How to make sure, that the date format in a variable of varchar is 'DD-MON-YYYY' . If there is anything else, exception needs to be raised. DATE datatype have NO format. if s <> to_char (to_date (s, 'DD-MON-YYYY'), 'DD-MON-YYYY') then

Is dd-mmm-yyyy a valid variable name?

Since dd-MMM-yyyy is not a valid variable name, I'd like to name it the standard. For example, I have an entry called ISO8601 for yyyy-MM-dd'T'HH:mm:ss.SSS and another called TSQL6 for dd MMM yy.


1 Answers

Rather than try to shim IE7, I think the simplest solution would be to write your own sort function and then call it for all the columns where the date format is used. This is pretty easy to do with DataTables, outlined here:

http://www.datatables.net/plug-ins/sorting

You can manually define the table to use the new sorting algorithm for your column, but that's a little clumsy, as you will need to do this wherever you use the format. Instead, you can autodetect, as outlined here:

http://www.datatables.net/plug-ins/type-detection

I created a fork of your fiddle with a quick solution here - I just converted the dates to a simple number - as long as you trust the data, this should be fine. Otherwise, you might want to convert to actual date objects, which is probably a more robust way to do it.

http://jsfiddle.net/pFdyK/

Code:

// Put these somewhere better than a global :)
var months = "JAN_FEB_MAR_APR_MAY_JUN_JUL_AUG_SEP_OCT_NOV_DEC".split("_");
function monthToOrd(month) {
    return $.inArray(month, months);
}
function customDateToOrd(date) {
    // Convert to a number YYYYMMDD which we can use to order
    var dateParts = date.split(/-/);
    var day = dateParts[0];
    var month = monthToOrd(dateParts[1]);
    var year = dateParts[2];
    var numericDate = (year * 10000) + (month * 100) + day;
    return numericDate;
}

// This will help DataTables magic detect your custom format
// Unshift so that it's the first data type (overridding in built ones)
jQuery.fn.dataTableExt.aTypes.unshift(
    function ( sData )
    {
        // You might want to make this check a little tighter so you don't match
        // invalid dates, but this should do for now
        if (sData.match(/\d{2}-[A-Za-z]{3}-\d{4}/))
            return 'custom-date';
        else
            return null;
    }
);

// define the sorts
jQuery.fn.dataTableExt.oSort['custom-date-asc']  = function(a,b) {
    var ordA = customDateToOrd(a);
    var ordB = customDateToOrd(b);
    return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
};

jQuery.fn.dataTableExt.oSort['custom-date-desc'] = function(a,b) {
    var ordA = customDateToOrd(a);
    var ordB = customDateToOrd(b);
    return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
};

$(document).ready(function() {
    $('html').addClass('lt-ie9');
   $('table.datatable').dataTable();
} );
like image 160
Shaun McCarthy Avatar answered Sep 29 '22 07:09

Shaun McCarthy