Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.