...

View Full Version : Add a Year to a Date



frogoth
08-10-2005, 08:35 PM
I have a form with an <input> initialDate (mm/dd/yyyy).
Later in the same form the initialDate auto-populates an <input> expireDate.
That's it. My user must manually change the year portion from 2005 to 2006.

I want the expireDate to auto-populate with the initialDate, but 1 year out.

I have been unable to find javascript to do that. :( Can anyone help?

Brandoe85
08-10-2005, 08:41 PM
You could get the current year and then add 1 to it:


var date = new Date();
var endYear = date.getFullYear()+1;


Good luck

frogoth
08-10-2005, 09:03 PM
Thanks Brando! :)

Let's say I am a relative newbie to javascript. Right!
Where would I put your bit of wisdom?

Sample code as it currently works without the additional year:
<form action="updateLog.cfm" method="post" name="logForm" onSubmit="return validate_form();">
<input type="text" value="" name="initialDate">
<input type="text" value="" name="expireDate" onFocus="if (this.value == ''){this.value=(initialDate.value)}"

I hope I can still use your code.

Brandoe85
08-10-2005, 09:28 PM
You can just add that right in however you'd like:


<input type="text" value="" name="expireDate" onFocus="var date = new Date(); if (this.value == ''){this.value = date.getFullYear()+1}"


Good luck

frogoth
08-10-2005, 09:59 PM
Yippee! It works!

But, it only gives me the Year. I guess I need to make my requests clearer.
Sorry! :D

The initialDate is mm/dd/yyyy (i.e., 08/10/2005), and
the expireDate should also be mm/dd/yyyy (i.e., 08/10/2006)

Brandoe85
08-10-2005, 10:17 PM
Where is the inital date coming from? Is it just todays date? or is it entered in, generated from the server?

Willy Duitt
08-10-2005, 10:25 PM
Try this:



<script type="text/javascript">
function addYear(date){
var date = date.split('/');
date[2] = date[2]/1+1;
return date.join('/');
}
</script>
</head>

<body>
<form>
<input type="text" name="initialDate" value="08/10/2005"
onchange="this.form['expireDate'].value=addYear(this.value)">
<input type="text" name="expireDate">
</form>


.....Willy

frogoth
08-11-2005, 03:23 PM
Good Morning Brando and Willy,
Nice try, but I think I need to be more precise in explaining exactly what I need.

Brando
In answer to your question, the administrator inputs the initialDate as "Date Issued", and the code is as follows:

Date Issued: <input type="text" value="" name="initialDate" onFocus="javascript:vDateType='1'" onChange="DateFormat(this,this.value,event,false,'1')" onBlur="DateFormat(this,this.value,event,true,'1')"> (mm/dd/yyyy)The onFocus, on Change, and on Blur forces the input format to be mm/dd/yyyy.

Later in the form as the administrator clicks on the Expiration Date input box, it auto-populates with the initialDate. The code for that action is as follows:

Expiration Date: <input type="text" value="" name="expireDate" onFocus="if (this.value == ''){this.value=(initialDate.value)}"> (mm/dd/yyyy)The administrator must then change the year portion of the date to 1 year out, i.e., from 08/10/2005 to 08/10/2006.

I want the Expiration Date to auto-populate with the initialDate + 1 Year, so the administrator does not need to change the year portion. The first code you provided accomplished that, but only auto-populated the year, not the entire date. (I apologize for not being clear.)

Willie
I don't believe I can incorporate your onChange, since I now have an onChange in that <input> tag.
Please note the initalDate code above.

As also stated above, the onFocus, onChange, and onBlur forces the input format to be mm/dd/yyyy.

Without this code, an administrator not following the format example of mm/dd/yyyy, input mm/dd/yy
which returned 01/01/1900 for both initialDate and expireDate when the Submit button was clicked.

Since the data is an audit trail, the administrator is not allowed edit that input. I had to go into the SQL server table and fix it.


Thanks for your contributions to my knowledge base.
I hope this explanation is of assistance. If either of you has any other questions, bring 'em on.

Willy Duitt
08-11-2005, 03:39 PM
I would suggest either combining both functions into one or call my function once your date format passes...


Good Luck;
.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum