Skip to content

Instantly share code, notes, and snippets.

@MikeSel
Created August 11, 2013 10:45
Show Gist options
  • Save MikeSel/6204361 to your computer and use it in GitHub Desktop.
Save MikeSel/6204361 to your computer and use it in GitHub Desktop.
Return correctly formatted date
getFormattedDate('1/1/2013 12:30:00');
var result;
function getFormattedDate(input) {
var pattern = /(.*?)\/(.*?)\/(.*?)$/;
result = input.replace(pattern, function (match, p1, p2, p3) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Dec'];
return (p2 < 10 ? "0" + p2 : p2) + " " + months[(p1 - 1)] + " " + p3;
});
}
alert(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment