JQuery Date Problem - Adding 1 Year
Hi,
I am using a jQuery datepicker to select a contract start date, and from this i want it to automatically calculate the contract end date which should be 1 year from the start date. I am using the following code to try todo this:
- $('#dtecontractstart').change(function() {
- // Get the date from the #dtecontractstart
- sContractStart = $('#dtecontractstart').val();
-
- // Split the string on the /
- var arr = sContractStart.split('/');
- // Change the array so the format is yyyy,mm,dd
- var newdate = arr[2]+","+arr[1]+","+arr[0];
-
- var myDate = new Date(newdate);
-
- var ContractEndDay = myDate.getDate()- 1
- if (ContractEndDay<10) ContractEndDay="0"+ContractEndDay;
-
- var ContractEndMonth = myDate.getMonth()+ 1
- if (ContractEndMonth<10) ContractEndMonth="0"+ContractEndMonth;
- var ContractEndYear = myDate.getFullYear()+ 1
-
- ContractEndDate = ContractEndDay + "/" + ContractEndMonth + "/" + ContractEndYear;
- $('#dtecontractend').val(ContractEndDate);
- });
This seems to work fine if the date slected is 02/03/2011, as it uses the above and brings back 01/03/2011....
However if i select 01/03/2011, it should return 28/02/2011, but i get 00/03/2012...
Any ideas why, also i am new to jQuery and have built the above, is the code correct or is there a much easier way of doing this?
Thanks in advance,
D