Date calculator, add or subtract days to or from a given date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dlx_son@yahoo.com

    Date calculator, add or subtract days to or from a given date

    Here is the code so far

    <form name="thisform" >

    <h3>Enter time to add to or subtract from:</h3>
    (If not entered, current time will be used)<br>

    Day: <input name="d1" alt="Day of month" size=3>
    Month: <input name="m1" alt="Month" size=3>
    Year: <input name="y1" alt="Year" size=5> (4 digits for year, e.g.
    1950)<br>
    Hours: <input name="h1" alt="Hours" size=3>

    Minutes: <input name="i1" alt="Minutes" size=3>
    Seconds: <input name="s1" alt="Seconds" size=3>
    <h3>Enter time to add/subtract</h3>
    <select name="type">
    <option value="add">Add
    <option value="sub">Sub tract
    </select>
    <input alt="Amount to add/subtract" name=amount size=10>
    <select name="unit">
    <option value="d">days
    <option value="h">hours
    <option value="m">minut es
    <option value="s">secon ds
    </select>

    (Note: Value to add or subtract must currently be less than 300
    years)<br>
    <input type=submit alt="Calculate" value="Calculat e new date!">
    </form>

    I would like to click on the submit button and then some kind of alert
    window would pop up. Or just write it on the page.

    thanks.

  • Lee

    #2
    Re: Date calculator, add or subtract days to or from a given date

    dlx_son@yahoo.c om said:[color=blue]
    >
    >Here is the code so far[/color]
    [color=blue]
    >I would like to click on the submit button and then some kind of alert
    >window would pop up. Or just write it on the page.[/color]

    What class is this for?

    Comment

    • Samir

      #3
      Re: Date calculator, add or subtract days to or from a given date

      It's not for a class,

      Here's the Original Code, I am trying to change to use in a more useful
      form for me, and ebay too.

      <form>
      <input type="text" name="date1" size="20"><br>
      date (yyyy/mm/dd)<br><br>
      <input type="text" name="date2" size="20" readonly><br>
      90 days from date<br><br>
      <input type="button" value="Calculat e"
      onclick="plus90 (this);">
      </form>

      <script type="text/javascript">

      document.forms( 0).date1.value = dateToString(ne w Date())

      function dateToString(d) {
      return [d.getYear(),d.g etMonth()+1,d.g etDate()].join('/')

      }

      function plus90(x){
      d = new Date(x.form.dat e1.value)
      d = new Date(d.getYear( ),d.getMonth(), d.getDate()+90)
      x.form.date2.va lue = dateToString(d)

      }

      </script>

      Comment

      • RobG

        #4
        Re: Date calculator, add or subtract days to or from a given date

        Samir wrote:
        [...][color=blue]
        > <form>[/color]

        <form action="">

        [...][color=blue]
        >
        > document.forms( 0).date1.value = dateToString(ne w Date())[/color]

        document.forms[0].date1.value = dateToString(ne w Date())
        --------------^-^

        <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>
        [color=blue]
        >
        > function dateToString(d) {
        > return [d.getYear(),d.g etMonth()+1,d.g etDate()].join('/')[/color]

        getYear() will return the year from 1900 (unless you are using
        IE, which does not properly implement getYear). You should
        either use getFullYear() or for wider support, create your own
        full year function:

        return [
        myFullYear(d.ge tYear()),
        d.getMonth()+1,
        d.getDate()
        ].join('/')

        function myFullYear(y){
        return (y<1900)?y+1900 :y;
        }
        [color=blue]
        > }
        >
        > function plus90(x){
        > d = new Date(x.form.dat e1.value)
        > d = new Date(d.getYear( ),d.getMonth(), d.getDate()+90)[/color]

        Why generate an entirely new date?

        function plus90(x){
        d = new Date(x.form.dat e1.value)
        d.setDate(d.get Date()+90);

        [...]

        given the above, subtracting days should be simple easy. As for
        time, you should convert the change in hours, minutes and
        seconds to milliseconds, then use:

        var changeInMillise conds = 1000 * (
        hoursEntered *60*60
        + minutesEntered *60
        + secondsEntered * 1);

        d.setTime(d.get Time() + changeInMillise conds);


        secondsEntered needs to be converted from a string to number,
        hence *1, which is a nice way of doing it in sync with the other
        variables.

        Using milliseconds this way to adjust the time is not perfect,
        but pretty darn close for short time intervals. I've also
        tossed in a function to add leading zeros to single digit
        numbers in the date string. Full code below.


        --
        Rob

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <title>Time play</title>
        <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-1">

        <script type="text/javascript">
        function dateToString(d) {
        var dateString = myFullYear(d.ge tYear())
        + '/' + addZ(d.getMonth ()+1)
        + '/' + addZ(d.getDate( ))
        + ' ' + addZ(d.getHours ())
        + ':' + addZ(d.getMinut es())
        + ':' + addZ(d.getSecon ds())
        return dateString;
        }

        function myFullYear(y){
        return ( y < 1900 )? y + 1900 : y;
        }

        function plus90(x){
        d = new Date(x.form.dat e1.value)
        d.setDate(d.get Date()+90);
        x.form.date2.va lue = dateToString(d) ;
        document.getEle mentById('msgBo x').innerHTML = dateToString(d) ;
        }

        function addTime(x){
        var tBit = x.form.timeChan ge.value.split( ':');
        var changeInMillise conds = 1000 * (
        tBit[0] *60*60
        + tBit[1] *60
        + tBit[2] * 1);

        d.setTime(d.get Time() + changeInMillise conds);
        x.form.date2.va lue = dateToString(d) ;
        document.getEle mentById('msgBo x').innerHTML = dateToString(d) ;
        }

        function addZ(z) {
        return (z<10)? '0' + z : z;
        }

        </script>

        </head>

        <body>
        <form action="">
        <input type="text" name="date1" size="20"><br>
        date (yyyy/mm/dd)<br><br>
        <input type="text" name="date2" size="20" readonly><br>
        90 days from date<br>
        <input type="text" name="timeChang e" size="20"
        value="03:12:37 ">
        <br>
        <input type="button" value="Add 90 days"
        onclick="plus90 (this);">
        <br>
        <input type="button" value="Add time"
        onclick="addTim e(this);">
        <br>
        <input type="reset">
        </form>
        <span id="msgBox"></span>

        <script type="text/javascript">
        document.forms[0].date1.value = dateToString(ne w Date())
        </script>

        </body>
        </html>

        Comment

        • RobG

          #5
          Re: Date calculator, add or subtract days to or from a given date

          RobG wrote:
          [...][color=blue]
          > function myFullYear(y){
          > return (y<1900)?y+1900 :y;
          > }[/color]

          Ooops, the above works fine in Mozilla-based browsers for all
          relevant (post 1 AD) years, however it will fail in IE for dates
          below 1900. A better (but not perfect) myFullYear is:

          function myFullYear(y){
          return ( y < 999 )? y + 1900 : y;
          }

          This still works fine in the 'Zillas, but will give erroneous
          results in IE for years before 999. Presumably any usage related
          to eBay does not require coverage for this time frame. :-x

          For further information, read here:

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

          --
          Rob

          Comment

          • Dr John Stockton

            #6
            Re: Date calculator, add or subtract days to or from a given date

            JRS: In article <1109279414.067 200.49780@g14g2 000cwa.googlegr oups.com>,
            dated Thu, 24 Feb 2005 13:10:14, seen in news:comp.lang. javascript,
            Samir <dlx_son@yahoo. com> posted :
            [color=blue]
            >Here's the Original Code, I am trying to change to use in a more useful
            >form for me, and ebay too.
            >
            ><form>
            > <input type="text" name="date1" size="20"><br>
            > date (yyyy/mm/dd)<br><br>
            > <input type="text" name="date2" size="20" readonly><br>
            > 90 days from date<br><br>
            > <input type="button" value="Calculat e"
            > onclick="plus90 (this);">
            ></form>
            >
            ><script type="text/javascript">
            >
            >document.forms (0).date1.value = dateToString(ne w Date())
            >
            >function dateToString(d) {
            > return [d.getYear(),d.g etMonth()+1,d.g etDate()].join('/')
            >
            >}
            >
            >function plus90(x){
            > d = new Date(x.form.dat e1.value)
            > d = new Date(d.getYear( ),d.getMonth(), d.getDate()+90)
            > x.form.date2.va lue = dateToString(d)
            >
            >}
            >
            ></script>[/color]


            You should validate the input date. In a Murrican-infested world, you
            must expect to get dates written as 2/25/05, whatever you ask for.
            Validation can return a Date Object. But perhaps you have not yet
            bothered to read the newsgroup FAQ with care? See sig.

            Function dateToString should give YYYY-MM-DD, since that is the
            standard. So you should use a leading zero function. I doubt whether
            it is worth creating an array and then joining it; I would use
            with (d)
            return getFullYear()+'-'+LZ(getMonth() +1)+'-'+LZ(getDate())

            Input should match that, so use .replace(/-/g, "/") or similar; but,
            in combining getting an Object with validation, that becomes irrelevant.

            It is strange that you have learned that new Date() accepts
            extended-range fields but are not using setDate() in that manner -
            d.setDate(d.get Date()+90)

            Better, IMHO, to pass this.form into plus90()

            Better to use a named form rather than relying on it being forms(0)
            - should that be forms[0] anyway?

            --
            © 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

            • Dr John Stockton

              #7
              Re: Date calculator, add or subtract days to or from a given date

              JRS: In article <VIwTd.30$Zn.33 82@news.optus.n et.au>, dated Fri, 25 Feb
              2005 03:12:53, seen in news:comp.lang. javascript, RobG
              <rgqld@iinet.ne t.auau> posted :[color=blue]
              >
              > function plus90(x){
              > d = new Date(x.form.dat e1.value)
              > d.setDate(d.get Date()+90);[/color]

              That gives the same local time (if it exists; 1 chance in 8766 currently
              that it does not) 90 days on.

              [color=blue]
              > given the above, subtracting days should be simple easy. As for
              > time, you should convert the change in hours, minutes and
              > seconds to milliseconds, then use:
              >
              > var changeInMillise conds = 1000 * (
              > hoursEntered *60*60
              > + minutesEntered *60
              > + secondsEntered * 1);
              >
              > d.setTime(d.get Time() + changeInMillise conds);[/color]

              That moves ahead by a certain absolute interval.

              If the hours entered are 90*24 = 2160, then there is about a 50% chance,
              in many locations, that it will not give the same result as plus90.

              The OP should be aware of this.

              --
              © 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

              • Dr John Stockton

                #8
                Re: Date calculator, add or subtract days to or from a given date

                JRS: In article <o_wTd.32$Zn.34 36@news.optus.n et.au>, dated Fri, 25 Feb
                2005 03:31:32, seen in news:comp.lang. javascript, RobG
                <rgqld@iinet.ne t.auau> posted :[color=blue]
                > A better (but not perfect) myFullYear is:
                >
                > function myFullYear(y){
                > return ( y < 999 )? y + 1900 : y;
                > }[/color]


                For current dates, return 2000 + y%100 works with all three
                behaviours of getYear() that I've heard of.

                --
                © 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

                • Lubna tt

                  #9
                  Re: Date calculator, add or subtract days to or from a given date


                  Lubnatariq@hotm ail.com


                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  Working...