Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Location
    Pacific Time Zone
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Add a Year to a Date

    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?

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You could get the current year and then add 1 to it:
    Code:
    var date = new Date();
    var endYear = date.getFullYear()+1;
    Good luck

  • #3
    New to the CF scene
    Join Date
    Aug 2005
    Location
    Pacific Time Zone
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You can just add that right in however you'd like:
    Code:
    <input type="text" value="" name="expireDate" onFocus="var date = new Date(); if (this.value == ''){this.value = date.getFullYear()+1}"
    Good luck

  • #5
    New to the CF scene
    Join Date
    Aug 2005
    Location
    Pacific Time Zone
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yippee! It works!

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

    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)

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Where is the inital date coming from? Is it just todays date? or is it entered in, generated from the server?

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    <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

  • #8
    New to the CF scene
    Join Date
    Aug 2005
    Location
    Pacific Time Zone
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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:
    Code:
    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.

  • #9
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would suggest either combining both functions into one or call my function once your date format passes...


    Good Luck;
    .....Willy


  •  

    Posting Permissions

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