Created
February 16, 2011 11:23
-
-
Save johnhunter/829231 to your computer and use it in GitHub Desktop.
Static Date methods for formatting js Date objects to strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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