Results 1 to 4 of 4

Thread: HELP! form for subtracting time!

  1. #1
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation HELP! form for subtracting time!

    I want to know how to make a form to subtract time from dates
    Like someoen fills out a form for March 8th, 2000, you can subtract time by 5 years to make it March 8th 1995. How do you do that?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm sure this will come in for some criticism for being overly simplistic but, it fulfills your request*:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    function subtractYear(dateIn, adjust){
    dateIn=dateIn.split(' ')
    dateIn[2]=dateIn[2]-adjust
    return dateIn.join(' ')
    }
    
    </script>
    </head>
    <body>
    <input id="dt" type="text" value="March 8th, 2000"><br>
    <input type="button" onclick="document.getElementById('dt').value=subtractYear(document.getElementById('dt').value, 5)">
    </body>
    </html>
    *Actual date math really is not required and can be quite complex cross browser, particulary when rendering back to a uniform date string. Problems will arise if the date filled into the form is not in (or close to in) the format you are expecting (the format you gave as an example). How do you know what format will be entered? Does your form exert some control over that?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What if im trying to do months? Like subtract 10 months from the date? THANKS SO MUCH IF U CAN HELP ME!
    Last edited by Greentea; 12-18-2005 at 03:13 AM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I try to make it a policy of not going too much further with a concept if I cannot get my questions answered. I usually only ask a question if my answer would be influenced by its answer. This was one of those sorts of questions:

    Quote Originally Posted by myself
    How do you know what format (the date, sic) will be entered (in, sic)? Does your form exert some control over that?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •