End date calculation from DatePicker and Term input
I am using a datepicker and have created 3 input fields.
- <label>Start Date:</label>
- <input type='text' name='StartDate' id='StartDate' />
- <label>Term:</label>
- <input type='text' name='Term' id='Term' />
- <label>End Date:</label>
- <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:
- var ContractTerm = $('#Term').val();
However if i manually set the term, it works correctly:
- var ContractTerm = 2;
But I need to be able to change the term.
Please see the jQuery code below.
- $(document).ready(function() {
- // DATEPICKER DATES 7 END CONTRACT CALCULATION
- $(function() {
- $( "#StartDate, #EndDate" ).datepicker({
- defaultDate: "+1w",
- dateFormat: 'dd/mm/yy',
- changeMonth: true,
- numberOfMonths: 1,
-
- onSelect: function( selectedDate ) {
- if(this.id == 'StartDate'){
- var ContractTerm = $('#Term').val();
- //var ContractTerm = 3;
-
- var dateMin = $('#StartDate').datepicker("getDate");
- var rMax = new Date(dateMin.getFullYear() + ContractTerm, dateMin.getMonth(),dateMin.getDate() - 1);
- $('#EndDate').val($.datepicker.formatDate('dd/mm/yy', new Date(rMax)));
- }
- }
- });
- });
- });
- Thanks,