www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] add year to the date

  1. #1
    Join Date
    May 2011
    Posts
    19

    resolved [RESOLVED] add year to the date

    Hello
    I have form like this
    HTML Code:
    <form name"form1">
    <input type="text" name="year1" id="year1" /> 
    <select name="status" >
    <option value="year">add year</option>
    <option value="month">add month</option>		
    <option></option>		
                        </select>
     <input type="text" name="year2" id="year2" /> </form>
    i want to add date at the first textfield(year1)at format"Y-m-d" and when choose from dropdown menu add year or month
    and display it at the second(year2) in the same format"Y-m-d"
    any help?

  2. #2
    Join Date
    Jul 2005
    Location
    USA
    Posts
    912
    Code:
    <script>
    
    function updateField(sel)
    {
      var choice = sel.options[sel.selectedIndex].value;
      
      if(choice != '')
      {
        var d = new Date(document.getElementById('year1').value);
        
        if(choice == 'year')
        {
          d.setFullYear(d.getFullYear() + 1);
        }
        else
        {
          d.setMonth(d.getMonth() + 1);
        }
    
    		d.setDate(d.getDate() + 1);
    
        document.getElementById('year2').value = d.getFullYear() + '-' + (d.getMonth()+1) + '-'+ d.getDate();
        
      } 
    }
    
    </script>
    
    <form name"form1">
    <input type="text" name="year1" id="year1" /> 
    <select name="status" onchange="updateField(this)">
    	<option value="">-- please choose --</option>
    	<option value="year">add year</option>
    	<option value="month">add month</option>		
    </select>
    <input type="text" name="year2" id="year2" /> 
    </form>

  3. #3
    Join Date
    May 2011
    Posts
    19

    Thumbs up

    thanks Webnerd ,
    it works good now and add year and month but The text field(year1) does not accept the date, but using the format(mm-dd-yyyy) and if using another format give me (NaN-NaN-NaN)
    and i want to insert date in this format(Y-m-d)

  4. #4
    Join Date
    May 2011
    Posts
    19
    thanks it work good now

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles