December 2009 Archives

The jQuery TableSorter plugin comes with built-in support for two ISO date formats via RegEx:

  1. Long such as: January 6, 1978 9:12 AM or Jan. 6, 2001 9:12 AM

    ^[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)))$
    
  2. Short such as: 1/6/78

    \d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}
    

Because I wanted to use the format of “Jan 6, 1978” I had to write my own parser. I could have just modified the default RegEx, but since I could guarantee the format of the input date, I could create a simpler RegEx:

// TableSort parser for date format: Jan 6, 1978
$.tablesorter.addParser({
  id: 'monthDayYear',
  is: function(s) {
      return false;
  },
  format: function(s) {
      var date = s.match(/^(\w{3})[ ](\d{1,2}),[ ](\d{4})$/);
      var m = monthNames[date[1]];
      var d = String(date[2]);
      if (d.length == 1) {d = "0" + d;}
      var y = date[3];
      return '' + y + m + d;
  },
  type: 'numeric'
});
var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";

This can be easily adapted for other date formats. To assist with testing with Firebug, place these two lines at the end of the “format” function to output input date and created string:

console.log(date);
console.log('' + y + m + d);

Enjoy.

About this site

The personal website of Beau Smith: Adventure seeker, designer, coder, culinary craftsman, athlete, lover.

Movable Type Plugins

  • Heading Anchors - Provides a template tag modifier to update all html headings with an id and anchor linking to the id.
  • ProCommunity - Three new template sets merging the best of the Community Blog, Community Forum, and the Professional template sets.
  • EntrySetBasename - A utility plugin to assist in setting entry basenames to the value of the dirified entry title.
  • Vanilla - A quiver of template sets focusing on specific features in Movable Type
  • Ghostwriter - Assign credit and get thanks for your assistance. A plugin for selecting Author on Edit Entry/Page screen.
  • TidyTags - A perl script to normalize Movable Type template tags
  • DashOff - Dashboard widget with links to get you where you want quickly.