jQuery in Action
Writing custom utility functions
195
7.3.2 Writing a date formatter
If you've come to the world of client-side programming from the server, one of
the things you may have longed for is a simple date formatter; something that the
JavaScript
the things you may have longed for is a simple date formatter; something that the
JavaScript
Date
type doesn't provide. Because such a function would operate on a
Date
instance, rather than any
DOM
element, it's a perfect candidate for a utility
function. Let's write one that uses the following syntax:
The implementation of this function is shown in listing 7.2. We're not going to go
into great detail regarding the algorithm used to perform the formatting (after
all, this isn't an algorithms book), but we're going to use this implementation to
into great detail regarding the algorithm used to perform the formatting (after
all, this isn't an algorithms book), but we're going to use this implementation to
Function syntax: $.formatDate
$.formatDate(date,pattern)
Formats the passed date according to the supplied pattern. The tokens that are substituted
in the pattern are as follows:
in the pattern are as follows:
yyyy: the 4-digit year
yy: the 2-digit year
MMMM: the full name of the month
MMM: the abbreviated name of the month
MM: the month number as a 0-filled, 2-character field
M: the month number
dd: the day in the month as a 0-filled, 2-character field
d: the day in the month
EEEE: the full name of the day of the week
EEE: the abbreviated name of the day of the week
a: the meridium (AM or PM)
HH: the 24-hour clock hour in the day as a 2-character, 0-filled field
H: the 24-hour clock hour in the day
hh: the 12-hour clock hour in the day as a 2-character, 0-filled field
h: the 12-hour clock hour in the day
mm: the minutes in the hour as a 2-character, 0-filled field
m: the minutes in the hour
ss: the seconds in the minute as a 2-character, 0-filled field
s: the seconds in the minute
S: the milliseconds in the second as a 3-character, 0-filled field
yy: the 2-digit year
MMMM: the full name of the month
MMM: the abbreviated name of the month
MM: the month number as a 0-filled, 2-character field
M: the month number
dd: the day in the month as a 0-filled, 2-character field
d: the day in the month
EEEE: the full name of the day of the week
EEE: the abbreviated name of the day of the week
a: the meridium (AM or PM)
HH: the 24-hour clock hour in the day as a 2-character, 0-filled field
H: the 24-hour clock hour in the day
hh: the 12-hour clock hour in the day as a 2-character, 0-filled field
h: the 12-hour clock hour in the day
mm: the minutes in the hour as a 2-character, 0-filled field
m: the minutes in the hour
ss: the seconds in the minute as a 2-character, 0-filled field
s: the seconds in the minute
S: the milliseconds in the second as a 3-character, 0-filled field
Parameters
date
(Date) The date to be formatted.
pattern
(String) The pattern to format the date into. Any characters not matching pat-
tern tokens are copied as-is to the result.
tern tokens are copied as-is to the result.
Returns
The formatted date.
The formatted date.