View Full Version : HELP! form for subtracting time!

12-17-2005, 09:59 PM
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?

12-17-2005, 11:56 PM
I'm sure this will come in for some criticism for being overly simplistic but, it fulfills your request*:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function subtractYear(dateIn, adjust){
dateIn=dateIn.split(' ')
return dateIn.join(' ')

<input id="dt" type="text" value="March 8th, 2000"><br>
<input type="button" onclick="document.getElementById('dt').value=subtractYear(document.getElementById('dt').value, 5)">

*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?

12-18-2005, 02:58 AM
What if im trying to do months? Like subtract 10 months from the date? :confused: THANKS SO MUCH IF U CAN HELP ME!

12-18-2005, 04:36 AM
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:

How do you know what format (the date, sic) will be entered (in, sic)? Does your form exert some control over that?