Skip to content

Instantly share code, notes, and snippets.

@johnhunter
Created February 16, 2011 11:23
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 johnhunter/829231 to your computer and use it in GitHub Desktop.
Save johnhunter/829231 to your computer and use it in GitHub Desktop.
Static Date methods for formatting js Date objects to strings
/*
Module Date.to - Static Date methods for formatting js Date objects to strings.
@author John Hunter
created 2010-03-30
use: Date.to.ddmmyyyy(new Date(), '^') => '30^03^2010'
*/
Date.to = (function () {
function ddmmyyyy (d, sp) {
if (d.constructor !== Date) return d;
return toShort(sp || '/', d.getDate(), d.getMonth()+1, d.getFullYear());
}
function hhmmss (d, sp) {
if (d.constructor !== Date) return d;
return toShort(sp || ':', d.getHours(), d.getMinutes(), d.getSeconds());
}
function toShort () {
var args = Array.prototype.slice.call(arguments),
sp = args.shift(),
i = args.length;
while(i--) args[i] = pack(args[i]);
return args.join(sp);
}
function pack (n) { return (n < 10)? '0' + n : n; }
return {
ddmmyyy: ddmmyyy, hhmmss: hhmmss
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment