Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
silly question on Date value evaluation or comparison
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
justaguy  
View profile  
 More options Sep 25 2012, 8:21 pm
Newsgroups: comp.lang.javascript
From: justaguy <lichunshe...@gmail.com>
Date: Tue, 25 Sep 2012 17:21:29 -0700 (PDT)
Local: Tues, Sep 25 2012 8:21 pm
Subject: silly question on Date value evaluation or comparison
Hi,

HTML code:
<input type="text" id="dateField">

js code:
                var dateValue = new
Date(document.getElementById('dateField').value);
                switch (dateValue) {
                        case '9/25/2012':
                                alert ('case 1 here');
                        {
                          dosomething here..
                        }

                       default:
                             alert("I'm not doing it right");
                  }

Comment:
I've entered the value of "9/25/2012" (of course without quotes) for
the dateField entry, but the case switch returns to default, so, I
guess 9/25/2012  is not the value for a date, then what?

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christoph Becker  
View profile  
 More options Sep 25 2012, 8:36 pm
Newsgroups: comp.lang.javascript
From: Christoph Becker <cmbecke...@gmx.de>
Date: Wed, 26 Sep 2012 02:36:35 +0200
Local: Tues, Sep 25 2012 8:36 pm
Subject: Re: silly question on Date value evaluation or comparison

justaguy wrote:
> I've entered the value of "9/25/2012" (of course without quotes) for
> the dateField entry, but the case switch returns to default, so, I
> guess 9/25/2012  is not the value for a date, then what?

See
<https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_...>:

| The JavaScript date is measured in milliseconds since midnight
| 01 January, 1970 UTC.

BTW: you should terminate your case clauses with break, if they are not
otherwise terminated, to avoid a fall-through.

--
Christoph M. Becker


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
justaguy  
View profile  
 More options Sep 25 2012, 9:39 pm
Newsgroups: comp.lang.javascript
From: justaguy <lichunshe...@gmail.com>
Date: Tue, 25 Sep 2012 18:39:28 -0700 (PDT)
Local: Tues, Sep 25 2012 9:39 pm
Subject: Re: silly question on Date value evaluation or comparison
On Sep 25, 8:36 pm, Christoph Becker <cmbecke...@gmx.de> wrote:

This is very helpful, thank you.  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"?

Also, yeah, I know each case needs the break statement at its end to
stop it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jukka K. Korpela  
View profile  
 More options Sep 26 2012, 12:57 am
Newsgroups: comp.lang.javascript
From: "Jukka K. Korpela" <jkorp...@cs.tut.fi>
Date: Wed, 26 Sep 2012 07:57:08 +0300
Local: Wed, Sep 26 2012 12:57 am
Subject: Re: silly question on Date value evaluation or comparison

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/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gregor Kofler  
View profile  
 More options Sep 26 2012, 3:11 am
Newsgroups: comp.lang.javascript
From: Gregor Kofler <use...@gregorkofler.com>
Date: Wed, 26 Sep 2012 09:11:03 +0200
Local: Wed, Sep 26 2012 3:11 am
Subject: Re: silly question on Date value evaluation or comparison
Am 2012-09-26 02:21, schrieb justaguy:> Hi,
 >
 > HTML code:
 > <input type="text" id="dateField">
 >
 > js code:
 >           var dateValue = new
 > Date(document.getElementById('dateField').value);
 >           switch (dateValue) {
 >                   case '9/25/2012':
 >                           alert ('case 1 here');
 >                   {
 >                            dosomething here..
 >                          }
 >
 >                         default:
 >                               alert("I'm not doing it right");
 >                    }
 >
 > Comment:
 > I've entered the value of "9/25/2012" (of course without quotes) for
 > the dateField entry, but the case switch returns to default, so, I
 > guess 9/25/2012  is not the value for a date, then what?

Why not RTFM? The date object generation is your first problem, your
next one will be the comparison in the case statement.

And RTFM would most likely solve a bunch of your future "problems", too.

Gregor


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Sep 27 2012, 4:27 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Thu, 27 Sep 2012 21:27:56 +0100
Local: Thurs, Sep 27 2012 4:27 pm
Subject: Re: silly question on Date value evaluation or comparison
In comp.lang.javascript message <k3u1v3$kq...@dont-email.me>, Wed, 26
Sep 2012 07:57:08, Jukka K. Korpela <jkorp...@cs.tut.fi> posted:

>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 writes a theoretician from the back of beyond - a nice place
(mosquitoes apart) certainly; but isolated.

The Standard permits, but does not require, entirely implementation-
dependent parsing, after trying 15.9.1.15.

The real world is aware that all or almost all browsers are either
written in America or are written with the US market, and MS IE
compatibility, firmly in mind.

Therefore, not only will the weird US date format be supported, but it
will be AT LEAST as reliable as any other date format.

The best format to use, however, is "2012/09/25".  It has all of the
advantages of ISO 8601 except for compliance, and I don't recall it
failing with new Date() in any browser.  But, if using other code,
beware as treating "09" as Octal Zero.

The OP needs to consider whether the whole of that day should be
matched, and whether the offset from GMT might vary.

If only the "calendar" date is wanted, then it is best to use UTC
methods throughout.

NEVER use new Date() except for the measurement of intervals of time.
Use instead new Date(0), which gives consistent results and ought to be
faster.

Query : What do the "Safari" parts of
<http://www.merlyn.demon.co.uk/js-datex.htm> report when using Safari 6
or higher?

--
 (c) John Stockton, nr London, UK.    ?...@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RobG  
View profile  
 More options Oct 3 2012, 3:12 am
Newsgroups: comp.lang.javascript
From: RobG <rg...@iinet.net.au>
Date: Wed, 3 Oct 2012 00:12:35 -0700 (PDT)
Local: Wed, Oct 3 2012 3:12 am
Subject: Re: silly question on Date value evaluation or comparison
On Friday, 28 September 2012 06:27:56 UTC+10, Dr J R Stockton  wrote:
[...]

> NEVER use new Date() except for the measurement of intervals of time.

Things like:

  var d = new Date(2012,9,3)

are entirely consistent in every browser, I see no need to use any other method to create a date object.

> Use instead new Date(0), which gives consistent results and ought to be
> faster.

If the requirement is to create a date object for 1970-01-01T00:00:00Z, then you might be correct (even ignoring the obvious "faster than what?"). But that is likely an infrequent scenario.

Usually a date object is required to be set to a specific value other than the epoch, so what is the benefit to creating the object then setting its value (requireing at least one and maybe two further calls) over creating the object and setting its value in one call?

--
Rob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Oct 4 2012, 2:36 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Thu, 4 Oct 2012 19:36:44 +0100
Local: Thurs, Oct 4 2012 2:36 pm
Subject: Re: silly question on Date value evaluation or comparison
In comp.lang.javascript message <dea8cd7c-f0b0-4fb9-ad55-22e749e17e31@go
oglegroups.com>, Wed, 3 Oct 2012 00:12:35, RobG <rg...@iinet.net.au>
posted:

>On Friday, 28 September 2012 06:27:56 UTC+10, Dr J R Stockton  wrote:
>[...]
>> NEVER use new Date() except for the measurement of intervals of time.

>Things like:

>  var d = new Date(2012,9,3)

>are entirely consistent in every browser, I see no need to use any
>other method to create a date object.

That is not new Date().  And IMHO new Date("2012/10/03") is better than
new Date(2012,9,3), because it looks like what it means (and in my FF
15.0.1, WinXPsp3, it seems about 10% faster).

That format works in every browser that I've tried; no-one has reported
that, in any of my pages, it fails; and I've mentioned it often enough
here without being told of any failures either.  Granted, the ECMA specs
do not *require* it to work.  And it is good to show non-FFF dates to
the chronologically backward.

>> Use instead new Date(0), which gives consistent results and ought to be
>> faster.

>If the requirement is to create a date object for 1970-01-01T00:00:00Z,
>then you might be correct (even ignoring the obvious "faster than
>what?").

Simple grammar - new Date(0) is faster than new Date(), since the latter
must consult the operating system for the date and maybe convert it into
ms from 1970.0 UTC.  At least, it is substantially faster in Windows,
and seems unlikely to be slower on any system.

> But that is likely an infrequent scenario.

It is infrequently needed; but, to judge from what I see, it is much
less infrequently used.

>Usually a date object is required to be set to a specific value other
>than the epoch, so what is the benefit to creating the object then
>setting its value (requireing at least one and maybe two further calls)
>over creating the object and setting its value in one call?

Agreed; but only the better sort of people realise that.

--
 (c) John Stockton, nr London, UK.    ?...@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Sauyet  
View profile  
 More options Oct 5 2012, 4:21 pm
Newsgroups: comp.lang.javascript
From: Scott Sauyet <scott.sau...@gmail.com>
Date: Fri, 5 Oct 2012 13:21:22 -0700 (PDT)
Local: Fri, Oct 5 2012 4:21 pm
Subject: Re: silly question on Date value evaluation or comparison
Dr J R Stockton wrote:

> [ ... ] IMHO new Date("2012/10/03") is better than
> new Date(2012,9,3), because it looks like what it means (and in my FF
> 15.0.1, WinXPsp3, it seems about 10% faster).

> That format works in every browser that I've tried; no-one has reported
> that, in any of my pages, it fails; and I've mentioned it often enough
> here without being told of any failures either.  Granted, the ECMA specs
> do not *require* it to work.  And it is good to show non-FFF dates to
> the chronologically backward.

It is generally faster in implementations I've tested:

    <http://jsperf.com/date-constructor-comparison>

It's very slightly faster in FF15 and IE8, very slightly slower in
FF10, and much faster in Chr19, all on WinXPsp3.  It's also much
faster on my Android phone.

But that's not the end of the story.  If you were doing this from
source code, I would grant you that this is a much cleaner way to
construct Dates.  But if you need to construct dates from existing
data, there are other issues to consider.  This:

    var year = 2012, month = 9, day = 3;
    var date = new Date(2012, 9, 3);

is much cleaner than this:

    var year = 2012, month = 9, day = 3;
    var oneIndexedMonth = month + 1;
    var date = new Date(year + "/" + (oneIndexedMonth < 10 ? "0" : "")
+
                   oneIndexedMonth + "/" + (day < 10 ? "0" : "") +
day);

and the performance is much less clear:

    <http://jsperf.com/date-constructor-comparison2>

Here the string-building costs enough to drive the performance well
below that of the multi-parameter constructor technique, at least in
FF and IE.  In Chrome, the single String, even when built this way, is
somewhat faster (some JIT compiling here?), and in Android it's still
substantially faster.

So I don't think the choice is as clear as you suggest, except in the
case that you're constructing a Date object in the source code.

  -- Scott


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Oct 6 2012, 2:06 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Sat, 6 Oct 2012 19:06:45 +0100
Local: Sat, Oct 6 2012 2:06 pm
Subject: Re: silly question on Date value evaluation or comparison
In comp.lang.javascript message <c1c0835f-07fb-4ba1-943e-627a24f1dcd4@n9
g2000yqn.googlegroups.com>, Fri, 5 Oct 2012 13:21:22, Scott Sauyet
<scott.sau...@gmail.com> posted:

And the case I was considering was indeed that of writing a fixed date
in the source code.  Where the date is in variables Y M D, constructing
a string makes little sense - but it can often be seen in pages written
by amateurish coders, such as are employed in PR departments.

I recall a page of one national laboratory (foreigners) which actually
used three digit years in JavaScript for recent dates ... admittedly in
comment.  They also constricted a date string from the results of
getFullYear, getUTCmonth, ... with amusing results at year rollover.
That code is gone; IIRC they now use Jquery or suchlike.

Does anyone here read Latin?

--
 (c) John Stockton, nr London, UK.    ?...@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evertjan.  
View profile  
 More options Oct 7 2012, 4:03 am
Newsgroups: comp.lang.javascript
From: "Evertjan." <exxjxw.hannivo...@inter.nl.net>
Date: 07 Oct 2012 08:03:15 GMT
Local: Sun, Oct 7 2012 4:03 am
Subject: Re: silly question on Date value evaluation or comparison
Dr J R Stockton wrote on 06 okt 2012 in comp.lang.javascript:

> And the case I was considering was indeed that of writing a fixed date
> in the source code.  Where the date is in variables Y M D, constructing
> a string makes little sense - but it can often be seen in pages written
> by amateurish coders, such as are employed in PR departments.

> I recall a page of one national laboratory (foreigners) which actually

"foreigners" as in "Americans"?

Tha BBC used 2 character years ["MM"] 12 years ago.

> used three digit years in JavaScript for recent dates ... admittedly in
> comment.  They also constricted a date string from the results of
> getFullYear, getUTCmonth, ... with amusing results at year rollover.
> That code is gone; IIRC they now use Jquery or suchlike.

> Does anyone here read Latin?

Nonnumquam.

Noli reparare in scripto javano,
quod non ruptus,
nisi causa turpitudinis.

Turpitudo stupiditasque
causae primae erratorum.

Necessitas consilii ad decimexione.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patricia Shanahan  
View profile  
 More options Oct 7 2012, 4:48 am
Newsgroups: comp.lang.javascript
From: Patricia Shanahan <p...@acm.org>
Date: Sun, 07 Oct 2012 09:48:26 +0100
Local: Sun, Oct 7 2012 4:48 am
Subject: Re: silly question on Date value evaluation or comparison

I more-or-less understand most of this, but what does "decimexione" mean?

Google translate and Perseus latin dictionary did not find it. I tried a
begins-with search for "decimex" in the Perseus word study tool, and it
also missed. A separate search for "mex" also failed.

Also, a suggestion. I was unsure, before I considered context, about
whether "scripto javano" mean "Java writing" or "JavaScript". A word
like JavaScript that is often written as one word with two capital
letters could be translated to a compound word like "jusurandum".

Patricia


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evertjan.  
View profile  
 More options Oct 7 2012, 6:09 am
Newsgroups: comp.lang.javascript
From: "Evertjan." <exxjxw.hannivo...@inter.nl.net>
Date: 07 Oct 2012 10:09:20 GMT
Local: Sun, Oct 7 2012 6:09 am
Subject: Re: silly question on Date value evaluation or comparison
Patricia Shanahan wrote on 07 okt 2012 in comp.lang.javascript:

search for "cimex"

> Google translate and Perseus latin dictionary did not find it. I tried
> a begins-with search for "decimex" in the Perseus word study tool, and
> it also missed. A separate search for "mex" also failed.

In Latin, as in Greek, Hebrew, etc, first seperate the stem from the
usual pre- and postpositions, the correct for possible ablaut [vowel
change caust by those]. "de-" is more probable than "deci-", as Latin did
not have decimal fractions as prepositions.

> Also, a suggestion. I was unsure, before I considered context, about
> whether "scripto javano" mean "Java writing" or "JavaScript". A word
> like JavaScript that is often written as one word with two capital
> letters could be translated to a compound word like "jusurandum".

I don't like that, "scripto javano" ablativus of "scriptum javanum",
Javanes script was my choice.

Latin does not make many ad-hoc compounds, and certainly has no incling
about inword capitalisation, it doesn't consider capitalisation at all.

Java is a multilingual island I frequent, Java, also a seperate language
from Javascript, in javascript seems to come from a type of coffee
[mod-L. coffea] not quite apt for translation.

"scriptum" for script is also a bit stretched.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »