Date.prototype.format

John Tamplin jat at google.com
Fri May 27 09:58:26 PDT 2011


On Fri, May 27, 2011 at 12:21 PM, Marc Harter <wavded at gmail.com> wrote:

> > Note that it is rarely useful to format with a fixed pattern, because
> > different locales have different norms for ordering of the fields,
> > separators, etc.
>
> By fixed pattern do you mean like  d.format('YY/mm/dd')?  If so,
> doesn't that allow for more expressive power?
>

To illustrate the problem, consider formatting a date next week with
"M/d/y", giving 6/1/2011.  US readers will understand that readily.
 However, if a European user sees that, they will interpret it as if it were
"d/M/y", and they will think it is in January.  Also, many locales have
different separators for the portions of the date, or insert additional
words in the longer formats, which won't be present if you just used a fixed
format pattern.

Instead, you should use a skeleton pattern of "Mdy" saying that you want a
format with numeric values for month day and year.  Then, you get the proper
format for the locale your user is running your app in.  Even better, though
more restrictive, is to use a predefined format where experts in each locale
have said this is a good "short" date format for that locale, since there
are different conventions.

For examples from CLDR, see:

   -
   http://unicode.org/cldr/trac/browser/tags/release-2-0/common/main/en.xml#L1259
   -
   http://unicode.org/cldr/trac/browser/tags/release-2-0/common/main/en_GB.xml#L57
   -
   http://unicode.org/cldr/trac/browser/tags/release-2-0/common/main/zh.xml#L1166
   -
   http://unicode.org/cldr/trac/browser/tags/release-2-0/common/main/en.xml#L1217
   -
   http://unicode.org/cldr/trac/browser/tags/release-2-0/common/main/zh.xml#L1123

Certainly, there are cases where you do want a fixed format (and in fact you
don't want localized names, but rather the English month/day names), such as
when formatting an RFC2822 date, and in those cases you do want to be able
to use a fixed format string.

-- 
John A. Tamplin
Software Engineer (GWT), Google
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110527/b4b7de4c/attachment.html>


More information about the es-discuss mailing list