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
  • 23,933
  • 14
  • 88
  • 109
bzbz
  • 182
  • 3
  • 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
  • 55,303
  • 33
  • 133
  • 164
  • @praveen... thanks for your effort bro...!! but the end date is picking the same date as the start date!! – bzbz Nov 26 '13 at 07:17
  • @bzbz I have spotted the wrong fiddle link , check this http://jsfiddle.net/praveen_jegan/LJ93d/4/ Let me know if it works ;) – Praveen Nov 26 '13 at 07:18
  • 2
    @praveen.. It works absolutely fine ...thanks for ya help.. cheers bro..!! – bzbz Nov 26 '13 at 09:28