On 2008-09-20 00:30, Lasse Reichstein Nielsen wrote:
There isn't a date formatter built in. For most cases, it's not worth
it anyway. Just make one function to format your dates as you want
it and use that wherever it's needed.
Here's one simple way to retro-fit a date formatting method. It
implements a subset of formats from strftime(3); the PHP function of the
same name is also similar. Some of the formats are missing, like the
names of months, week days, etc, because they were not required when the
method was written. Note: some of the formats are different from what
you might expect - read the comments and adjust the code as you see fit.
Usage:
var d = new Date(2008, 3, 4);
var dstr1 = d.fmt("%m/%d/%Y");
var dstr2 = d.fmt("%d.%m.%Y ");
/* a simple date formatting method */
Date.prototype. fmt = function (format) {
var buffer = "";
var chr, nxt;
for (var i = 0, len = format.length; i < len; ++i) {
chr = format.substr(i , 1);
if (chr != "%") {
buffer += chr;
continue;
}
nxt = format.substr(i + 1, 1);
if (nxt == "C") { // 2-digit century (eg "19" or "20")
buffer += String(Math.flo or( /* this should be one line */
this.getFullYea r() / 100)).padFront( "0", 2);
} else if (nxt == "d") { // day of month (01 to 31)
buffer += String(this.get Date()).padFron t("0", 2);
} else if (nxt == "F") { // month of year (1 to 12)
buffer += (this.getMonth( ) + 1);
} else if (nxt == "f") { // day of month (1 to 31)
buffer += this.getDate();
} else if (nxt == "H") { // 24h hours (00 - 23)
buffer += String(this.get Hours()).padFro nt("0", 2);
} else if (nxt == "M") { // minutes (00 - 59)
buffer += String(this.get Minutes()).padF ront("0", 2);
} else if (nxt == "m") { // month of year (01 to 12)
buffer += String(this.get Month() + 1).padFront("0" , 2);
} else if (nxt == "n") { // newline
buffer += "\n";
} else if (nxt == "R") { // 24h time (%H:%M)
buffer += String(this.get Hours()).padFro nt("0", 2)
+ ":"
+ String(this.get Minutes()).padF ront("0", 2);
} else if (nxt == "S") { // seconds (00 - 59)
buffer += String(this.get Seconds()).padF ront("0", 2);
} else if (nxt == "T") { // 24h time (%H:%M:%S)
buffer += String(this.get Hours()).padFro nt("0", 2)
+ ":"
+ String(this.get Minutes()).padF ront("0", 2)
+ ":"
+ String(this.get Seconds()).padF ront("0", 2);
} else if (nxt == "Y") { // 4-digit year
buffer += this.getFullYea r();
} else if (nxt == "y") { // 2-digit year
var y = String(this.get FullYear());
buffer += y.substr(y.leng th -2, 2);
} else if (nxt == "%") { // literal "%" character
buffer += "%";
} else {
buffer += format.substr(i , 2);
}
++i; // skip next
}
return buffer;
}
In this form, it also requires an extension to the String prototype object:
/* left-pads the string with {chr} until it is {len} characters long */
String.prototyp e.padFront = function (chr, len) {
if (this.length >= len) {
return this;
} else {
return chr.repeat(len - this.length) + this;
}
}
/* returns the string repeated {n} times */
String.prototyp e.repeat = function (n) {
if (n < 1) return "";
return (new Array(n + 1)).join(this);
}
If you don't like to mess with the native String and Date types, you
could refactor these methods as simple global methods (with date and
string arguments, respectively).
- Conrad