Comparing Date values

Discussion in 'Javascript' started by Kermit Piper, Apr 14, 2006.

  1. Kermit Piper

    Kermit Piper Guest

    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
    Kermit Piper, Apr 14, 2006
    #1
    1. Advertising

  2. Kermit Piper

    Hal Rosser Guest

    "Kermit Piper" <> wrote in message
    news:...
    > 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.
    Hal Rosser, Apr 14, 2006
    #2
    1. Advertising

  3. Kermit Piper

    Kermit Piper Guest

    So, do you mean I should do something like :if (!dbDate) == true ?
    Kermit Piper, Apr 14, 2006
    #3
  4. Kermit Piper

    Randy Webb Guest

    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/
    Randy Webb, Apr 14, 2006
    #4
  5. Kermit Piper

    Hal Rosser Guest

    "Kermit Piper" <> wrote in message
    news:...
    > 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
    }
    Hal Rosser, Apr 14, 2006
    #5
  6. JRS: In article <>
    , dated Thu, 13 Apr 2006 19:43:43 remote, seen in
    news:comp.lang.javascript, Kermit Piper <>
    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.
    Dr John Stockton, Apr 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorgen Gustafsson
    Replies:
    4
    Views:
    571
    Jorgen Gustafsson
    Dec 12, 2003
  2. .Net Sports
    Replies:
    1
    Views:
    2,644
    intrader
    Jun 25, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  4. Les Juby
    Replies:
    3
    Views:
    197
    Bob Barrows [MVP]
    Apr 24, 2005
  5. anagai

    Comparing input date and system date

    anagai, Oct 19, 2007, in forum: Javascript
    Replies:
    5
    Views:
    140
    Thomas 'PointedEars' Lahn
    Oct 22, 2007
Loading...

Share This Page