Format function, in which you can pass a format string; my first approach was to issue a series of consecutive and "destructive" replace calls, but upon discovering that the 5.5 (or higher) version of JScript supported the use of a function as the
replaceText argument of the
replace method, I got creative.
Here's an example call of what I wanted:
SomeDiv.innerText = (new Date()).format('dddd, mmmm dd, yyyy.');
This would display:
Saturday, July 16, 2005
So in my first approach, I globally and case-insensitively replaced dddd with the corresponding string, which "destroyed" every occurrence, so that later in the code I could replace dd with the date number.
This worked just fine, but I knew that by inspecting the format specifier for a match, I could skip the search of every format specifier; say I only want the month and the date; well, by switching upon the format specifier (or rather "datepart" specifier), the year replacement will never be issued. Get it?
The fun part relies in the use of a function in the
replaceText argument of the
replace method; this way the
$1 property as a function argument always represents the last match.
Other considerations include the format or "datepart" specifiers: none other than
yyyy will be parsed as the year; months and days have the usual three flavors of fullname (
mmmm), three-letter (
mmm) or numeric (
mm); hours (
hh) can be rectified to the 12-hour format with the
a/p specifier, and minutes (
nn) and seconds (
ss) may also be specified.
WOFA, (Without Further Adou):
var gsMonthNames = new Array(
var gsDayNames = new Array(
Date.prototype.format = function(f)
return ' ';
var d = this;
case 'yyyy': return d.getFullYear();
case 'mmmm': return gsMonthNames[d.getMonth()];
case 'mmm': return gsMonthNames[d.getMonth()].substr(0, 3);
case 'mm': return (d.getMonth() + 1).zf(2);
case 'dddd': return gsDayNames[d.getDay()];
case 'ddd': return gsDayNames[d.getDay()].substr(0, 3);
case 'dd': return d.getDate().zf(2);
case 'hh': return ((h = d.getHours() % 12) ? h : 12).zf(2);
case 'nn': return d.getMinutes().zf(2);
case 'ss': return d.getSeconds().zf(2);
case 'a/p': return d.getHours() < 12 ? 'a' : 'p';
- A date with a value of 0 returns a non-breaking space.
- Notice how the
d variable is available to the replacement function (but the
this object is not).
- The regular expression looks for any of the bracketed pattern characters in a two+ sequence, or for the very specific a/p match.
- Defining names globally helps and serves other purposes, i.e. listing days in a calendar.
Hector J. Rivas has 25+ years of experience managing hardware and software development under many different operating systems, platforms and languages. He has developed microcontroller interfaces and PC games; authored computer based training lessons and delivered fully functional financial and administrative data-intensive applications, as well as image processing and other calculation-intensive applications. Mr. Rivas has also managed Y2K remediation, large scale platform migration and Web site projects, from R&D to actual deployment.