HTML and CSS Reference
Date.prototype.strftime mainly consists of two parts: the replace
function which takes care of replacing format specifiers with their corresponding
values, and the Date.formats object which is a collection of helpers. It can be
broken down as follows:
• Date.formats is an object with format specifiers as keys and methods to
extract the corresponding data from a date as values
Some format specifiers are convenient shortcuts to longer formats
• String.prototype.replace is used with a regexp that matches format
The replacer function checks if a given specifier is available on Date.
formats and uses it if it is, otherwise the specifier is left untouched (i.e.,
How would we go about testing this method? One way is to include the script
in our web page and use it where we need it and then verify manually if the website
displays dates correctly. If it doesn't work, we probably won't get a lot of hints as to
why, and are left debugging. A slightly more sophisticated approach (although not
by much) is to load it in a web page and pop open a console and play around with
it. Perhaps something like the session in Listing 1.3.
Listing 1.3 Manually checking code in Firebug
>>> var date = new Date(2009, 11, 5);
Uh-oh. Our Firebug session indicates all is not well with our strftime . This
means we'll have to investigate and rerun the test to verify that it's working. That's
more manual labor. We can do better. Let's create a minimal HTML page where we
load in the source script along with another script where we add some test code.
This way we can inspect the result of changes without having to retype the tests.
Listing 1.4 shows the HTML page that we'll use to test.