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

Discussion in 'Javascript' started by dlx_son@yahoo.com, Feb 24, 2005.

  1. Guest

    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">Subtract
    </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">minutes
    <option value="s">seconds
    </select>

    (Note: Value to add or subtract must currently be less than 300
    years)<br>
    <input type=submit alt="Calculate" value="Calculate 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.
     
    , Feb 24, 2005
    #1
    1. Advertising

  2. Lee Guest

    said:
    >
    >Here is the code so far


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


    What class is this for?
     
    Lee, Feb 24, 2005
    #2
    1. Advertising

  3. Samir Guest

    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="Calculate"
    onclick="plus90(this);">
    </form>

    <script type="text/javascript">

    document.forms(0).date1.value = dateToString(new Date())

    function dateToString(d){
    return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')

    }

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

    }

    </script>
     
    Samir, Feb 24, 2005
    #3
  4. RobG Guest

    Samir wrote:
    [...]
    > <form>


    <form action="">

    [...]
    >
    > document.forms(0).date1.value = dateToString(new Date())


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

    <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>

    >
    > function dateToString(d){
    > return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')


    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.getYear()),
    d.getMonth()+1,
    d.getDate()
    ].join('/')

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

    > }
    >
    > function plus90(x){
    > d = new Date(x.form.date1.value)
    > d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)


    Why generate an entirely new date?

    function plus90(x){
    d = new Date(x.form.date1.value)
    d.setDate(d.getDate()+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 changeInMilliseconds = 1000 * (
    hoursEntered *60*60
    + minutesEntered *60
    + secondsEntered * 1);

    d.setTime(d.getTime() + changeInMilliseconds);


    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.getYear())
    + '/' + addZ(d.getMonth()+1)
    + '/' + addZ(d.getDate())
    + ' ' + addZ(d.getHours())
    + ':' + addZ(d.getMinutes())
    + ':' + addZ(d.getSeconds())
    return dateString;
    }

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

    function plus90(x){
    d = new Date(x.form.date1.value)
    d.setDate(d.getDate()+90);
    x.form.date2.value = dateToString(d);
    document.getElementById('msgBox').innerHTML = dateToString(d);
    }

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

    d.setTime(d.getTime() + changeInMilliseconds);
    x.form.date2.value = dateToString(d);
    document.getElementById('msgBox').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="timeChange" 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="addTime(this);">
    <br>
    <input type="reset">
    </form>
    <span id="msgBox"></span>

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

    </body>
    </html>
     
    RobG, Feb 25, 2005
    #4
  5. RobG Guest

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


    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.demon.co.uk/js-date0.htm#gFY>

    --
    Rob
     
    RobG, Feb 25, 2005
    #5
  6. JRS: In article <>,
    dated Thu, 24 Feb 2005 13:10:14, seen in news:comp.lang.javascript,
    Samir <> posted :

    >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="Calculate"
    > onclick="plus90(this);">
    ></form>
    >
    ><script type="text/javascript">
    >
    >document.forms(0).date1.value = dateToString(new Date())
    >
    >function dateToString(d){
    > return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')
    >
    >}
    >
    >function plus90(x){
    > d = new Date(x.form.date1.value)
    > d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)
    > x.form.date2.value = dateToString(d)
    >
    >}
    >
    ></script>



    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.getDate()+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.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, Feb 25, 2005
    #6
  7. JRS: In article <VIwTd.30$>, dated Fri, 25 Feb
    2005 03:12:53, seen in news:comp.lang.javascript, RobG
    <> posted :
    >
    > function plus90(x){
    > d = new Date(x.form.date1.value)
    > d.setDate(d.getDate()+90);


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


    > 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 changeInMilliseconds = 1000 * (
    > hoursEntered *60*60
    > + minutesEntered *60
    > + secondsEntered * 1);
    >
    > d.setTime(d.getTime() + changeInMilliseconds);


    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.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr John Stockton, Feb 25, 2005
    #7
  8. JRS: In article <o_wTd.32$>, dated Fri, 25 Feb
    2005 03:31:32, seen in news:comp.lang.javascript, RobG
    <> posted :
    > A better (but not perfect) myFullYear is:
    >
    > function myFullYear(y){
    > return ( y < 999 )? y + 1900 : y;
    > }



    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.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, Feb 25, 2005
    #8
  9. Lubna tt Guest

    Lubna tt, Jun 26, 2005
    #9
    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. Manoj Nair
    Replies:
    4
    Views:
    21,426
    Manoj Nair
    Sep 4, 2003
  2. Lord0
    Replies:
    1
    Views:
    581
    Thomas Weidenfeller
    Apr 19, 2006
  3. Multibyte add & subtract

    , Apr 28, 2006, in forum: C Programming
    Replies:
    2
    Views:
    376
    Rod Pemberton
    Apr 29, 2006
  4. Replies:
    8
    Views:
    463
    osmium
    Oct 14, 2006
  5. clarken

    VHDL for add/subtract

    clarken, Dec 20, 2007, in forum: VHDL
    Replies:
    0
    Views:
    1,534
    clarken
    Dec 20, 2007
Loading...

Share This Page