Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16154

Re: silly question on Date value evaluation or comparison

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: silly question on Date value evaluation or comparison
Date 2012-09-26 07:57 +0300
Organization A noiseless patient Spider
Message-ID <k3u1v3$kq2$1@dont-email.me> (permalink)
References <f3ac39d0-7800-442d-ac95-f000b89e32b0@a11g2000yqb.googlegroups.com> <k3tim5$hl1$1@speranza.aioe.org> <46cb7029-4d6f-421a-a2f2-7d3d18de660b@e14g2000yqm.googlegroups.com>

Show all headers | View raw


2012-09-26 4:39, justaguy wrote:

> And yet, three methods, that appear
> most likely to get what I want, do not produce what I want.
> Namely,
> toDateString(), toLocaleDateString() and toLocaleFormat()
> what method would produce something like "9/25/2012"?

Before even considering that, consider how you are constructing the Date 
value. When you invoke new Date(...) with an argument like "9/25/2012", 
the effect is implementation-dependent. The string argument will be 
parsed with Date.parse(), about which clause 15.9.4.2 of the ECMAScript 
standard says:

"The function first attempts to parse the format of the String according 
to the rules called out in Date Time String Format (15.9.1.15). If the 
String does not conform to that format the function may fall back to any 
implementation-specific heuristics or implementation-specific date formats."

So the only date-time string format for which Date.parse() is required 
to have a defined, implementation-independent meaning is a specific 
interchange format defined in the standard. It is a simplification of 
the ISO 8601 format, YYYY-MM-DDTHH:mm:ss.sssZ. This is great for 
communication between software components, but for most human beings.

In order to reliably convert strings to Date values or vice versa, use a 
suitable library that can deal with the formats you want. People have 
different recommendations on this. My recommendation is Globalize.js
https://github.com/jquery/globalize
because it makes your code globalization-ready without much extra effort 
beyond what is needed to work with some particular format and because 
its use (both basic and advanced) is described in my book:
http://www.bytelevelbooks.com/books/global_javascript.html

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

silly question on Date value evaluation or comparison justaguy <lichunshen84@gmail.com> - 2012-09-25 17:21 -0700
  Re: silly question on Date value evaluation or comparison Christoph Becker <cmbecker69@gmx.de> - 2012-09-26 02:36 +0200
    Re: silly question on Date value evaluation or comparison justaguy <lichunshen84@gmail.com> - 2012-09-25 18:39 -0700
      Re: silly question on Date value evaluation or comparison "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-26 07:57 +0300
        Re: silly question on Date value evaluation or comparison Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-27 21:27 +0100
          Re: silly question on Date value evaluation or comparison RobG <rgqld@iinet.net.au> - 2012-10-03 00:12 -0700
            Re: silly question on Date value evaluation or comparison Dr J R Stockton <reply1240@merlyn.demon.co.uk.invalid> - 2012-10-04 19:36 +0100
              Re: silly question on Date value evaluation or comparison Scott Sauyet <scott.sauyet@gmail.com> - 2012-10-05 13:21 -0700
                Re: silly question on Date value evaluation or comparison Dr J R Stockton <reply1240@merlyn.demon.co.uk.invalid> - 2012-10-06 19:06 +0100
                Re: silly question on Date value evaluation or comparison "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-07 08:03 +0000
                Re: silly question on Date value evaluation or comparison Patricia Shanahan <pats@acm.org> - 2012-10-07 09:48 +0100
                Re: silly question on Date value evaluation or comparison "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-07 10:09 +0000
  Re: silly question on Date value evaluation or comparison Gregor Kofler <usenet@gregorkofler.com> - 2012-09-26 09:11 +0200

csiph-web