Comparing Date values

K

Kermit Piper

Hello,

I am comparing two date values, one from a database and one that has
been converted from a hard-coded string into an actual Date type. So
far so good. The problem I'm having is that one of the values comes
from the database, and for existing values it works fine, but if the
date doesn't exist (which will always be the condition when the user
first enters into the form) I am adding logic to my javascript like: if
(dbDate < staticDate || dbDate == ""), then we go into our validation
code. But it isn't working for a new form entry, only if the record
already exists. I think the problem is that because I'm comparing
actual Date values, the (dbDate == "") isn't going to work because ""
is a string value if I'm not mistaken, and this won't work against a
Date value.

So, my question is, what is the best way to check for a non-existent
Date object (value). Should I do something like (dbDate == null)? I'm
just not exactly sure how to check for a non-existent date object. Any
advice would really be appreciated.

Thanks,
KP
 
H

Hal Rosser

Kermit Piper said:
Hello,


So, my question is, what is the best way to check for a non-existent
Date object (value). Should I do something like (dbDate == null)? I'm
just not exactly sure how to check for a non-existent date object. Any
advice would really be appreciated.

if (!dbDate) (I think), will return true if dbDate does not exist.
 
R

Randy Webb

Kermit Piper said the following on 4/14/2006 12:25 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

So, do you mean I should do something like :if (!dbDate) == true ?

No, not the == true

if (!dbDate){
//dbDate should exist
}
 
H

Hal Rosser

Kermit Piper said:
So, do you mean I should do something like :if (!dbDate) == true ?
Like Randy says - (!dbDate) is *already* a boolean expression which is true
or false.
if (!dbDate){
// code if the dbDate does not exist
}
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Thu, 13 Apr 2006 19:43:43 remote, seen in
news:comp.lang.javascript said:
Hello,

I am comparing two date values, one from a database and one that has
been converted from a hard-coded string into an actual Date type. So
far so good. The problem I'm having is that one of the values comes
from the database, and for existing values it works fine, but if the
date doesn't exist (which will always be the condition when the user
first enters into the form) I am adding logic to my javascript like: if
(dbDate < staticDate || dbDate == ""), then we go into our validation
code. But it isn't working for a new form entry, only if the record
already exists. I think the problem is that because I'm comparing
actual Date values, the (dbDate == "") isn't going to work because ""
is a string value if I'm not mistaken, and this won't work against a
Date value.

So, my question is, what is the best way to check for a non-existent
Date object (value). Should I do something like (dbDate == null)? I'm
just not exactly sure how to check for a non-existent date object. Any
advice would really be appreciated.

I doubt whether that's the right question. A coder can, but need not,
want to test whether a coder-created Object exists.

As you've been told, one can test for the non-existence of an Object
called X by using if (!X) ... but that does seem to require
specification of an existing Object which might hold that Object, as in
if (document.all && !document.getElementById) ...
testing for all & getElementById .

To test whether a variable is defined, as one might want to do after
if (DoIt) var D = new Date(Str)
, one can use if (typeof D != "undefined") .

One must distinguish carefully, after the sole statement
var U
between X, which is a variable that has not been defined, and U, which
is a defined variable whose value has not been defined (its value
therefore being the special value <undefined>).

A Date Object D can certainly not exist (and hence be undefined); I
suspect that if it exists it cannot have an undefined value, though it
can have a conventionally-meaningless value (such as 0 representing
1970-01-01 00:00:00.000 GMT) or a value of NaN, which is what my
attempts to make it contain undefined or Infinity give.

A Date Object can be compared against a String; a direct comparison only
gives true if the string represents that date in the right format, but
testing +new Date(X) == "" is a valid, if silly, way to test if the
Date Object holds zero.

new Date(null) == new Date(0) != new Date(NaN) == NaN


Advice :

1) see signature, lines 2 3 4.
2) test existence before testing value


====

Bug in IE6? lastModified ??

<URL:http://www.merlyn.demon.co.uk/last-mod.htm> was last uploaded at
close to Tue 2006-02-21 23:36 GMT, genuine time, UK winter, UK then
using GMT. FTP shows the file on the server as being 20060221 23:36, OK
so far. My IE4 here shows the lastModified string as "02/21/06
23:36:05" which (apart from being YY & FFF) is OK - note, that's looking
TODAY, UK Summer, UK = GMT+1.

But, IIRC, the IE6? at the Public Library, looking YESTERDAY, UK Summer,
showed a lastModified string representing an hour later, 00:36:05 on the
22nd Feb (I forget the exact format).

It appears, therefore, that IE6 has read the UTC stored at the server,
and displayed what that civil time would have been if February had been
in Summer.

FWIW, the Library system seems to be US-configured, though by location
it should be UK and by majority user possibly KR.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top