Skip to content

Instantly share code, notes, and snippets.

@wesen
Created September 8, 2011 12:45
Show Gist options
  • Save wesen/1203315 to your computer and use it in GitHub Desktop.
Save wesen/1203315 to your computer and use it in GitHub Desktop.
/**
* Convert a mysql timestamp to javascript, taking the timezone into account.
**/
function mysqlToDate(timestamp) {
var months = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var regex = /^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])T([0-2][0-9]):([0-5][0-9]):([0-5][0-9])(.*)/;
var parts = timestamp.replace(regex,"$2/ $3, $1 $4:$5:$6 GMT$7").split('/');
var str = months[parseInt(parts[0])] + parts[1];
return new Date(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment