Skip to content

Instantly share code, notes, and snippets.

@numbcoder
Created May 28, 2013 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numbcoder/5660210 to your computer and use it in GitHub Desktop.
Save numbcoder/5660210 to your computer and use it in GitHub Desktop.
//dateFormat(new Date(), '%Y-%m-%d %H:%M:%S');
var dateFormat = function(date, fstr) {
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return date.getFullYear();
case '%m': m = date.getMonth() + 1; break;
case '%d': m = date.getDate(); break;
case '%H': m = date.getHours(); break;
case '%M': m = date.getMinutes(); break;
case '%S': m = date.getSeconds(); break;
default: return m.slice(1);
}
// add leading zero if required
return ('0' + m).slice(-2);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment