Compare Dates

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

    #1

    Compare Dates

    Thanks for the Help in my previous post.

    I've been working on this and it's almost what I want.

    I want to obtain the user's current age by comparing their date of birth
    (user inputs) to the current date.

    I know how to get the Current Date but I'm not finding how to calculate the
    Current Date minus the User's Birthday.

    It would be something like yourage = curdate - bday;

    I will then use the results to determine if the User is Over 21 or Under 21.

    Any help would be greatly appreciated!

    I'm going nuts trying to figure this out.

    Is there an Easier Book to learn this stuff other than the "Begining
    JavaScript 2nd Edition".

    Thanks again for any help. I'm a total novice.

    =============== ========

    <HTML>
    <HEAD>
    <TITLE>Legal Drinking Age </TITLE>
    <form>
    <input type="button" value="Submit" onClick="legalA ge()">
    </form>
    </HEAD>
    <BODY>
    <script language="JavaS cript" type="text/JavaScript">

    var curdate = new Date()
    document.write( curdate.getDate ())
    document.write( ".")
    document.write( curdate.getMont h() + 1)
    document.write( ".")
    document.write( curdate.getFull Year())
    function legalAge()
    {
    var ageString;
    var legalAge=21;
    var ageNum;
    ageString = prompt ("Please enter your age", " ");
    ageNum = parseInt(ageStr ing);
    document.write( ageString + "<br>");
    if (ageNum >= legalAge)
    {alert ("Let's Party!");}
    else if (ageNum < legalAge)
    {alert ("Sorry! You're too young to drink!");}
    else if (ageNum < 0)
    {alert ("Please enter a valid age"); }
    else
    {alert ("Please enter a valid age"); }
    }
    </SCRIPT>
    </BODY>
    </HTML>


  • Randy Webb

    #2
    Re: Compare Dates

    Rich wrote:
    [color=blue]
    > Thanks for the Help in my previous post.[/color]

    Not sure who you are referring to, but, I have to assume you are
    referring to me and not to John Stockton. In the future, please quote
    what you are replying to and if your new question is a continuation of a
    conversation, then don't start a new thread. It makes life simpler on
    everyone.
    [color=blue]
    > I've been working on this and it's almost what I want.
    >
    > I want to obtain the user's current age by comparing their date of birth
    > (user inputs) to the current date.
    > I know how to get the Current Date but I'm not finding how to calculate the
    > Current Date minus the User's Birthday.
    > It would be something like yourage = curdate - bday;[/color]

    It would be simpler to subtract 21 years from todays date, and then
    compare it to the bday.

    Create a new Date object with the birthdate parameters, then subtract
    and compare.

    Its simpler *not* to subtract dates though. Simply make a flow chart of
    how you would determine it manually:

    Step 1: Subtract years.
    Step 2: Is it over 21? Legal Age.
    Step 3: Is it under 21? Not Legal.
    Step 4: Is it 21? Check the Month.
    Step 5: Is the month prior, later or the same as this month?
    Step 6: If the month is prior, Legal Age.
    Step 7: If the month is later, not Legal.
    Step 8: If the month is the same, compare the day of the month.
    Step 9: Is the date earlier or the same? Legal Age.
    Step 10: Not Legal.

    Beware of people trying to make you learn more than you need to know at
    this point. Make life as simple as you can and learn as you go.
    [color=blue]
    > Is there an Easier Book to learn this stuff other than the "Begining
    > JavaScript 2nd Edition".[/color]

    Since I have never seen or read that book, I can't answer that.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • Rich

      #3
      Re: Compare Dates

      I must be tired.

      I read what you posted (thank you for the response) but I'm clueles as how
      I would make this happen.

      How would I compare? In other word what "function, variable or script"
      would allow me to do this?

      Are there any tutorials online that would help me better understand all of
      this?

      I understand some stuff but for the most part feel lost with other things.

      Thanks again,


      "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
      news:xfqdnWLOus 64DtrfRVn-qw@comcast.com. ..[color=blue]
      > Rich wrote:
      >[color=green]
      >> Thanks for the Help in my previous post.[/color]
      >
      > Not sure who you are referring to, but, I have to assume you are referring
      > to me and not to John Stockton. In the future, please quote what you are
      > replying to and if your new question is a continuation of a conversation,
      > then don't start a new thread. It makes life simpler on everyone.
      >[color=green]
      >> I've been working on this and it's almost what I want.
      >>
      >> I want to obtain the user's current age by comparing their date of birth
      >> (user inputs) to the current date.
      >> I know how to get the Current Date but I'm not finding how to calculate
      >> the Current Date minus the User's Birthday.
      >> It would be something like yourage = curdate - bday;[/color]
      >
      > It would be simpler to subtract 21 years from todays date, and then
      > compare it to the bday.
      >
      > Create a new Date object with the birthdate parameters, then subtract and
      > compare.
      >
      > Its simpler *not* to subtract dates though. Simply make a flow chart of
      > how you would determine it manually:
      >
      > Step 1: Subtract years.
      > Step 2: Is it over 21? Legal Age.
      > Step 3: Is it under 21? Not Legal.
      > Step 4: Is it 21? Check the Month.
      > Step 5: Is the month prior, later or the same as this month?
      > Step 6: If the month is prior, Legal Age.
      > Step 7: If the month is later, not Legal.
      > Step 8: If the month is the same, compare the day of the month.
      > Step 9: Is the date earlier or the same? Legal Age.
      > Step 10: Not Legal.
      >
      > Beware of people trying to make you learn more than you need to know at
      > this point. Make life as simple as you can and learn as you go.
      >[color=green]
      >> Is there an Easier Book to learn this stuff other than the "Begining
      >> JavaScript 2nd Edition".[/color]
      >
      > Since I have never seen or read that book, I can't answer that.
      >
      > --
      > Randy
      > comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly[/color]


      Comment

      • Fred Oz

        #4
        Re: Compare Dates

        Randy Webb wrote:[color=blue]
        > Rich wrote:[/color]
        [...][color=blue][color=green]
        >>
        >> I want to obtain the user's current age by comparing their date of
        >> birth (user inputs) to the current date.
        >> I know how to get the Current Date but I'm not finding how to
        >> calculate the Current Date minus the User's Birthday.
        >> It would be something like yourage = curdate - bday;[/color]
        >
        >
        > It would be simpler to subtract 21 years from todays date, and then
        > compare it to the bday.
        >
        > Create a new Date object with the birthdate parameters, then subtract
        > and compare.
        >
        > Its simpler *not* to subtract dates though. Simply make a flow chart of
        > how you would determine it manually:
        >
        > Step 1: Subtract years.
        > Step 2: Is it over 21? Legal Age.
        > Step 3: Is it under 21? Not Legal.
        > Step 4: Is it 21? Check the Month.
        > Step 5: Is the month prior, later or the same as this month?
        > Step 6: If the month is prior, Legal Age.
        > Step 7: If the month is later, not Legal.
        > Step 8: If the month is the same, compare the day of the month.
        > Step 9: Is the date earlier or the same? Legal Age.
        > Step 10: Not Legal.[/color]

        A much simpler algorithm is to subtract 21 years from today's date
        and see if the resulting date is bigger than the birth date:

        function checkAge(x) { // x = say '21/03/1984'
        x = x.split('/');
        var xDate = new Date(x[2],x[1]-1,x[0]);
        var tDate = new Date();
        tDate.setYear(t Date.getFullYea r() - 21);
        return (xDate <= tDate);
        }

        Send the birthday in dd/mm/yyyy format and the function returns true
        if the date is 21 years or more, otherwise false.

        I'm not sure how reliable you need to make this since the user can
        enter any date they like, but for the record you may need to allow
        for timezones, daylight saving and the vagaries of users' systems
        which may have the date set to anything (so perhaps pass your
        server's date as the seed for tDate).

        Also note that no validation of input is done, it assumes a valid
        date has been entered.
        [color=blue]
        >
        > Beware of people trying to make you learn more than you need to know at
        > this point. Make life as simple as you can and learn as you go.[/color]

        Information can be found at:

        <URL:http://www.merlyn.demo n.co.uk/js-date1.htm>


        [...]


        The full script in a page:

        <html>
        <head>
        <title>Over 21</title>
        </head>
        <body>

        <script type="text/javascript">
        function checkAge(x) { // x = say '21/03/1984'
        x = x.split('/');
        var xDate = new Date(x[2],x[1]-1,x[0]);
        var tDate = new Date();
        tDate.setYear(t Date.getFullYea r() - 21);
        return (xDate <= tDate);
        }
        </script>
        <form name="theForm" action="">
        <label for="bDay">Birt hday (dd/mm/yyyy)
        <input type="text" id="bDay" value="28/03/1984"></label>
        <br>
        Your system's current date:
        <script type="text/javascript">
        var nDate = new Date();
        document.write(
        nDate.getDate() + '/'
        + (+ nDate.getMonth( )+1) + '/'
        + nDate.getFullYe ar());
        </script>
        <br>
        <input type="button" value="21 or over?" onclick="
        alert(checkAge( this.form.bDay. value));
        ">
        <input type="reset">
        </form></body>
        </html>





        --
        Fred

        Comment

        • Dr John Stockton

          #5
          Re: Compare Dates

          JRS: In article <xfqdnWLOus64Dt rfRVn-qw@comcast.com> , dated Mon, 28 Mar
          2005 00:16:36, seen in news:comp.lang. javascript, Randy Webb
          <HikksNotAtHome @aol.com> posted :[color=blue]
          >Rich wrote:
          >[color=green]
          >> Thanks for the Help in my previous post.[/color]
          >
          >Not sure who you are referring to, but, I have to assume you are
          >referring to me and not to John Stockton.[/color]

          That's not obligatory; but it is permissible.
          [color=blue][color=green]
          >> I know how to get the Current Date but I'm not finding how to calculate the
          >> Current Date minus the User's Birthday.
          >> It would be something like yourage = curdate - bday;[/color]
          >
          >It would be simpler to subtract 21 years from todays date, and then
          >compare it to the bday.[/color]

          But not simplest.

          The current date is obtained, as milliseconds from 1970-01-01 00:00:00
          GMT, in a Date Object by D = new Date. To subtract 21 years it is
          necessary to extract the local year, subtract 21, and re-insert (though
          the book may say to add 1000*60*60*24*3 65.25 to the time :-<).

          The Birth Date is obtained by asking for (the month, the day, and) the
          year; one will be handling that, implicitly or explicitly, as a number
          of years, and 21 can very conveniently be added at that stage.


          The OP should however consider the case of Frederic of Penzance. If he,
          unlike Fred, lives in a country with a written constitution, then
          applicable legislation should deal with the point; if not, there should
          at least be duly established precedent.


          [color=blue][color=green]
          >> Is there an Easier Book to learn this stuff other than the "Begining
          >> JavaScript 2nd Edition".[/color]
          >
          >Since I have never seen or read that book, I can't answer that.[/color]

          Never trust a book with a spelling mistake in its own title.

          The easiest book is probably in the "Dummies" series, or one of its
          competitors. But, rather than looking for a book which will teach
          something easily, one should look for a book which will teach something
          correctly. The OP should read the newsgroup FAQ.


          Refs :
          <URL:http://www.merlyn.demo n.co.uk/leapyear.htm#Fr ed>
          <URL:http://www.merlyn.demo n.co.uk/js-date0.htm#Age>

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of 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

          • RobG

            #6
            Re: Compare Dates

            Rich wrote:[color=blue]
            > I must be tired.
            >
            > I read what you posted (thank you for the response) but I'm clueles as how
            > I would make this happen.
            >
            > How would I compare? In other word what "function, variable or script"
            > would allow me to do this?
            >[/color]

            Why compare anything? Create a date string for 'today' on your
            server. Subtract 21 from the year. Deliver it as text in your page
            in an unambiguous format, e.g. "29 March 1984".

            Tell users:

            "If you were born after 29 March 1984 you are not old enough
            to legally consume/purchase/whatever alcoholic beverages
            in /insert jurisdiction/ "

            or words to that effect.

            No client-side JavaScript, no validation, no date arithmetic.


            --
            Rob

            Comment

            • Randy Webb

              #7
              Re: Compare Dates

              Dr John Stockton wrote:[color=blue]
              > JRS: In article <xfqdnWLOus64Dt rfRVn-qw@comcast.com> , dated Mon, 28 Mar
              > 2005 00:16:36, seen in news:comp.lang. javascript, Randy Webb
              > <HikksNotAtHome @aol.com> posted :
              >[color=green]
              >>Rich wrote:
              >>
              >>[color=darkred]
              >>>Thanks for the Help in my previous post.[/color]
              >>
              >>Not sure who you are referring to, but, I have to assume you are
              >>referring to me and not to John Stockton.[/color]
              >
              >
              > That's not obligatory; but it is permissible.
              >[/color]

              Until someone discovers the secret to eternal life the only thing
              obligatory is death. Everything else is optional. But based on the 2
              replies, and the helpfulness to a newbe, I made my choice.
              [color=blue]
              >[color=green][color=darkred]
              >>>I know how to get the Current Date but I'm not finding how to calculate the
              >>>Current Date minus the User's Birthday.
              >>>It would be something like yourage = curdate - bday;[/color]
              >>
              >>It would be simpler to subtract 21 years from todays date, and then
              >>compare it to the bday.[/color]
              >
              >
              > But not simplest.[/color]

              For someone trying to learn the basic concepts of Javascript and Dates,
              yes, its simpler, and to me simplest.

              [color=blue]
              > The current date is obtained, as milliseconds from 1970-01-01 00:00:00
              > GMT, in a Date Object by D = new Date. To subtract 21 years it is
              > necessary to extract the local year, subtract 21, and re-insert (though
              > the book may say to add 1000*60*60*24*3 65.25 to the time :-<).
              > The Birth Date is obtained by asking for (the month, the day, and) the
              > year; one will be handling that, implicitly or explicitly, as a number
              > of years, and 21 can very conveniently be added at that stage.[/color]

              Subtract 21 or add 21, its irrelevant. The principle is the same.
              [color=blue]
              >
              > The OP should however consider the case of Frederic of Penzance. If he,
              > unlike Fred, lives in a country with a written constitution, then
              > applicable legislation should deal with the point; if not, there should
              > at least be duly established precedent.[/color]

              Precedence for what?

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

              Comment

              • Dr John Stockton

                #8
                Re: Compare Dates

                JRS: In article <w4KdnT7lc4dtR9 TfRVn-ow@comcast.com> , dated Tue, 29 Mar
                2005 17:47:36, seen in news:comp.lang. javascript, Randy Webb
                <HikksNotAtHome @aol.com> posted :[color=blue]
                >
                >Subtract 21 or add 21, its irrelevant. The principle is the same.[/color]

                But the practice is different; in this case an instance of the start
                year is supplied as a number, but the current year is supplied only
                encoded in an Object.


                [color=blue][color=green]
                >> The OP should however consider the case of Frederic of Penzance. If he,
                >> unlike Fred, lives in a country with a written constitution, then
                >> applicable legislation should deal with the point; if not, there should
                >> at least be duly established precedent.[/color]
                >
                >Precedence for what?[/color]

                How to deal with a situation resembling that of Frederic, of course.
                AIUI, his case was fully treated in New York, in 1879, chronologically
                in between Paignton & Paris; it was a most ingenious paradox.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                Comment

                • Randy Webb

                  #9
                  Re: Compare Dates

                  Dr John Stockton wrote:[color=blue]
                  > JRS: In article <w4KdnT7lc4dtR9 TfRVn-ow@comcast.com> , dated Tue, 29 Mar
                  > 2005 17:47:36, seen in news:comp.lang. javascript, Randy Webb
                  > <HikksNotAtHome @aol.com> posted :
                  >[color=green]
                  >>Subtract 21 or add 21, its irrelevant. The principle is the same.[/color]
                  >
                  >
                  > But the practice is different; in this case an instance of the start
                  > year is supplied as a number, but the current year is supplied only
                  > encoded in an Object.[/color]

                  Yes, and for a newbe it can be intriguing.
                  [color=blue]
                  >
                  >[color=green][color=darkred]
                  >>>The OP should however consider the case of Frederic of Penzance. If he,
                  >>>unlike Fred, lives in a country with a written constitution, then
                  >>>applicable legislation should deal with the point; if not, there should
                  >>>at least be duly established precedent.[/color]
                  >>
                  >>Precedence for what?[/color]
                  >
                  >
                  > How to deal with a situation resembling that of Frederic, of course.
                  > AIUI, his case was fully treated in New York, in 1879, chronologically
                  > in between Paignton & Paris; it was a most ingenious paradox.[/color]

                  Coming from someone who repeatedly says "Do not post URLs when a short
                  snippet/sample will suffice", that sounds hypocritical of you to rely
                  upon someone to visit a URL to understand what you are referring to.

                  As for a birthdate falling on the 29th of February causing problems, it
                  doesn't to anyone with any intelligence that uses a little common sense,
                  and does not introduce any paradox. Or, would you prefer to explain the
                  Paradox?


                  --
                  Randy
                  comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                  Comment

                  • Dr John Stockton

                    #10
                    Re: Compare Dates

                    JRS: In article <FbOdnQnI97yLet DfRVn-gg@comcast.com> , dated Fri, 1 Apr
                    2005 19:28:16, seen in news:comp.lang. javascript, Randy Webb
                    <HikksNotAtHome @aol.com> posted :[color=blue][color=green]
                    >> How to deal with a situation resembling that of Frederic, of course.
                    >> AIUI, his case was fully treated in New York, in 1879, chronologically
                    >> in between Paignton & Paris; it was a most ingenious paradox.[/color]
                    >
                    >Coming from someone who repeatedly says "Do not post URLs when a short
                    >snippet/sample will suffice", that sounds hypocritical of you to rely
                    >upon someone to visit a URL to understand what you are referring to.[/color]


                    Javascript code is on-topic for this newsgroup; indeed, it is its
                    purpose. An adequate explanation of Frederic's case is not - hence the
                    use of the Web reference, for those interested enough to go further.
                    [color=blue]
                    >As for a birthdate falling on the 29th of February causing problems, it
                    >doesn't to anyone with any intelligence that uses a little common sense,
                    >and does not introduce any paradox. Or, would you prefer to explain the
                    >Paradox?[/color]

                    It seems that you have not taken fullest advantage of the URL; that you
                    have neither followed the link that it contains, nor the traditional
                    printed version, and do not recall a performance. Sic transit gloria
                    G&S.




                    Only a little intelligence and common sense are required, as is also the
                    case for dealing correctly with the effects of Summer Time. But it is
                    also necessary to recollect the need to deal with the situations (cf.
                    Fred), and readers of this newsgroup will surely be aware that such
                    recollection frequently does not occur.

                    The OP needs to assure himself that his code will correctly handle a
                    case such as Fred's, in accordance with applicable legislation; or, at
                    least, show that he has recognised the potential ambiguity.

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                    Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                    PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                    Comment

                    Working...