views:

393

answers:

1

Using the jQuery tablesorter plugin . I wonder how could I make it work with dates in the full format:

"Jan 21, 2009 16:00"

Problem is that when that date (a string) has been localized with user current locale

"gen 21, 2009 16:00"

Do I have to write a custom sorter for each locale?

Thanks.

 `<table id="orders" class="sortable">
  <thead><tr><th>Da</th><th>Al</th><th class="right">Camere</th><th class="right">Spesa dell'ordine</th></tr></thead>
  <tr>
      <td>gen 21, 2009 22:00</td><td>gen 22, 2009 22:00</td><td class="right">1</td><td class="right">30.00€</td>´
  </tr>
A: 

Well, the Tablesorter plugin will detect that "gen 21, 2009' is a date column. It will then pass it to the javascript Date constructor to parse it; that might be the step that fails. (I don't know if the constructor accepts localized strings; you can test that by running this:

new Date("gen 21, 2009 16:00").getTime();

If it returns "NaN" (as it does on my en-US firefox), then you'll need a custom parser. If it returns 1232514000000 then you don't need to do anything.

Tablesorter will detect a column as "US long date" if it matches this regular expression:

/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/

a.k.a:

  • 3 - 10 alphabetic characters (month)
  • an optional period
  • a space
  • 1 - 2 digits (day)
  • comma, then space
  • 4-digit year, or apostrophe followed by 2-digit year
  • optional 24 hour time, or 12-hour time followed by uppercase AM or PM.
Stobor