MLNTN

Maniacal musings of a pixel perfectionist

Simple date conversion from SQL to readable

Posted by Jared on December 2, 2008

I needed a simple date converter function from SQL dates (YYYY-MM-DD) to something readable (M/D/YYYY).  So I wrote this little function.  Maybe someone else will have some use for it.

var makeSQLDatePretty = function(d) {
  return d.replace(/([0-9]{4})-0?([0-9]{1,2})-0?([0-9]{1,2})/, '$2/$3/$1');
};

Pass in a SQL date and the function trims leading zeros and returns a reformatted date.

var sqlDate = '2008-12-02';
alert(makeSQLDatePretty(sqlDate)); // should alert '12/2/2008'

Crazy Europeans may want to change the replace regex with ‘$3/$2/$1′.

Leave a Reply