2

I have 3 input fields all together.

  1. Contract period: 1 years(for example)
  2. start date : 30 - 1- 2012 (for example)
  3. end date : ????

(Can we get the end date automatically according to the contract period mentioned, which mean if the date after 1 year is 30-1-2013 can we get it automatically in the third field after mentioning the first and second field).

Stephen Ostermiller
  • 21,408
  • 12
  • 81
  • 104
bzbz
  • 182
  • 2
  • 13

1 Answers1

0

Possible, using onSelect option of jQuery datepicker.

1) get the value of contract year and parse it as integer.

var addYears = parseInt($('#contract').val(), 10); 

2) Split the selected date in startDate, as below

var t = date.split('/');

3) Now add the years and parse it as Date object.

var fin = new Date(parseInt(t[2], 10) + addYears, --t[0], t[1]);

Finally,

HTML:

In years only:
<input id="contract" type="text" />
<input id="start" type="text" />
<input id="end" type="text" />

JS:

$('#end').datepicker();
$('#start').datepicker({
    onSelect: function (date, args) {
        var addYears = parseInt($('#contract').val());
        var t = date.split('/');
        var fin = new Date(parseInt(t[2], 10) + addYears, --t[0], t[1]);
        $('#end').datepicker("setDate", fin);
    }
});

JSFiddle

Community
  • 1
  • 1
Praveen
  • 53,079
  • 32
  • 129
  • 156