End date calculation from DatePicker and Term input

End date calculation from DatePicker and Term input

I am using a datepicker and have created 3 input fields. 

  1. <label>Start Date:</label>
  2. <input type='text' name='StartDate' id='StartDate' />

  3. <label>Term:</label>
  4. <input type='text' name='Term' id='Term' />

  5. <label>End Date:</label>
  6. <input type='text' name='EndDate' id='EndDate' />

When a date is added into the StartDate and a number is added into term (Years), the end date should equal the StartDate plus the number of term years. Therefore if start date is 01/02/2013 and term is 2 years, the EndDate should be 31/01/2015.

I have created the jQuery code, but can not get the EndDate to calculate correctly when i try and get the value of the term input:
  1. var ContractTerm = $('#Term').val();

However if i manually set the term, it works correctly:
  1. var ContractTerm = 2;
But I need to be able to change the term.

Please see the jQuery code below.

  1. $(document).ready(function() {
  2. // DATEPICKER DATES 7 END CONTRACT CALCULATION
  3. $(function() {
  4. $( "#StartDate, #EndDate" ).datepicker({
  5. defaultDate: "+1w",
  6. dateFormat: 'dd/mm/yy',
  7. changeMonth: true,
  8. numberOfMonths: 1,
  9. onSelect: function( selectedDate ) {
  10. if(this.id == 'StartDate'){
  11. var ContractTerm = $('#Term').val();
  12. //var ContractTerm = 3;
  13. var dateMin = $('#StartDate').datepicker("getDate");
  14. var rMax = new Date(dateMin.getFullYear() + ContractTerm, dateMin.getMonth(),dateMin.getDate() - 1); 
  15. $('#EndDate').val($.datepicker.formatDate('dd/mm/yy', new Date(rMax)));                    
  16. }
  17. }
  18. });
  19. });
  20. });

  21. Thanks,