Comparing dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harlan Dorado

    Comparing dates

    Hi,
    I'm trying to write a function to compare a date posted from a text
    field in
    an asp-page to the date today. The idea is that the date entered in the
    form
    field cannot be in the past. This is my function:

    function chkDate(){
    var dDate = Date()
    if(document.frm Date.txtDate.va lue<dDate){
    alert("You've entered a date in the past, please try again.");
    return false;
    }
    }

    It seems to me that my script doesn't understand that I enter a date,
    because I always receive the alert - no matter what I put in the text
    field.
    I probably have to convert the value or something. Or perhaps I'm
    testing
    the wrong property....
    Being a novice in javascript I can certainly sit here and wonder, but
    some
    of you guys probably spot immediately what I do wrong and what needs to
    be
    changed.

    Thanks for any reply.

    Harlan



    ______________
    the woim toins


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Mike

    #2
    Re: Comparing dates

    Yeah what you got there is a comparison of the date object's string value
    agains the value of the text field. That will never work. You need to parse
    the text the user entered and create a new Date object based on those
    day/month/year values (see the javascript documentation for how to
    manipulate and create Date objects). This gets to be a pain if you allow
    international date formats because you have to account for all the different
    ways to enter a date (dd/mm/yy, dd/mm/yyyy, mm/dd/yy, etc)

    You can prob find some free date scripts on the net. Try scriptsearch.co m.



    Comment

    • kaeli

      #3
      Re: Comparing dates

      In article <400ae340$0$703 05$75868355@new s.frii.net>,
      harlandorado@sp ray.no enlightened us with...[color=blue]
      > function chkDate(){
      > var dDate = Date()
      > if(document.frm Date.txtDate.va lue<dDate){[/color]

      You're comparing a string to a date.

      Here's a hint.



      I don't use these functions, so I don't know how cross-browser they are.

      Don't forget to check for no value (blank text field) before trying to
      compare.

      --
      --
      ~kaeli~
      Jesus saves, Allah protects, and Cthulhu thinks you'd make
      a nice sandwich.



      Comment

      • Dr John Stockton

        #4
        Re: Comparing dates

        JRS: In article <WaudnQZ4_tk5op bdRVn-hQ@comcast.com> , seen in
        news:comp.lang. javascript, Mike <mike{removeToe mail}@synovic.c om> posted
        at Sun, 18 Jan 2004 20:23:32 :-[color=blue]
        >Yeah what you got there is a comparison of the date object's string value[/color]

        AIUI, he has no Date object. ECMA-262, 15.9.2.
        [color=blue]
        >agains the value of the text field. That will never work. You need to parse
        >the text the user entered and create a new Date object based on those
        >day/month/year values (see the javascript documentation for how to
        >manipulate and create Date objects). This gets to be a pain if you allow
        >internationa l date formats because you have to account for all the different
        >ways to enter a date (dd/mm/yy, dd/mm/yyyy, mm/dd/yy, etc)
        >
        >You can prob find some free date scripts on the net. Try scriptsearch.co m.[/color]

        If you read the FAQ *before* posting an "answer", you will be better
        informed.

        "Internatio nal date formats" is evidently an American term, intended to
        mean "foreign" ones. Do not use it. There is exactly one full
        International numeric Gregorian date format, YYYY-MM-DD, given by ISO
        8601 (a Federal standard, I believe). The word you should have used is
        "various".

        It is not *necessary* to parse anything. The input string *can* be
        required in, or converted to, standard form, and then compared as a
        string with an ISO representation of new Date().

        One cannot allow for all the date forms you list, without further
        information; for the first 12 days of a month, dd/mm/yy and mm/dd/yy
        cannot be told apart by mere inspection. However, yyyy?mm?dd is safe,
        because (a) it is a standard, (b) *no-one* uses yyyy?dd?mm.


        I cannot see in ECMA-262 a specification of the format of a numeric date
        string; mm/dd/yy (FFF) is common, but it may be unwise to assume that it
        will hold everywhere in perpetuity. Therefore, I generally prefer to
        split the date string (in known order) into its numeric fields and apply
        them in new Date(,,), unless the string is known to be YYYY?MM?DD.

        S = "23/04/1616"
        T = S.split(/\D+/)
        T = new Date(T[2], T[1]-1, T[0])
        D = new Date()

        document.write( "Bill is not yet dead : ", D < T )

        Since the OP is generating the date string to be checked against today,
        he can be sure of its format. He could instead generate the number of
        milliseconds since Epoch.

        The OP's function seems not to be able to return true.

        For a validation function, ISTM clearer to use the structure

        function CheckOK()
        var OK = <test as needed>
        if (!OK) { < actions as needed > }
        return OK }

        which ensures a defined return value.

        Be aware that comparison with today's date needs consideration; it *may*
        be better to use tomorrow's : T = new Date(T[2], T[1]-1, T[0] + 1)


        Date() gives a string in an ill-specified form; instead, new Date()
        should be used.

        Remember that a page served by a U.S. server in the local afternoon will
        be received in New Zealand on the following morning by their calendar;
        and a brand new one from NZ will generally arrive in HI on the previous
        date.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...