By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,727 Members | 766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,727 IT Pros & Developers. It's quick & easy.

Comparing Date values

P: n/a
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

Apr 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Kermit Piper" <di***********@hotmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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.
Apr 14 '06 #2

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

Apr 14 '06 #3

P: n/a
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.

<URL: http://www.safalra.com/special/googlegroupsreply/ >
So, do you mean I should do something like :if (!dbDate) == true ?


No, not the == true

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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '06 #4

P: n/a

"Kermit Piper" <di***********@hotmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
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
}
Apr 14 '06 #5

P: n/a
JRS: In article <11**********************@i39g2000cwa.googlegroups .com>
, dated Thu, 13 Apr 2006 19:43:43 remote, seen in
news:comp.lang.javascript, Kermit Piper <di***********@hotmail.com>
posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.