Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Created July 6, 2009 18:55
Show Gist options
  • Save atesgoral/141612 to your computer and use it in GitHub Desktop.
Save atesgoral/141612 to your computer and use it in GitHub Desktop.
Date.prototype.format = function (fmt) {
var date = this;
return fmt.replace(
/\{([^}:]+)(?::(\d+))?\}/g,
function (s, comp, pad) {
var fn = date["get" + comp];
if (fn) {
var v = (fn.call(date) +
(/Month$/.test(comp) ? 1 : 0)).toString();
return pad && (pad = pad - v.length)
? new Array(pad + 1).join("0") + v
: v;
} else {
return s;
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment