Javascript date function to calculate the end date using the duration

Hi,

 I need some pointers on how to calculate an end date by adding the duration (say 6 months) to the start date.

For eg: if i enter 01/11/2008 as start date and duration as 12 (months) it should show me end date as 31/10/2009.

Any help is highly appreciated.

-Murali*
LVL 13
Murali MurugesanFull stack Java developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SreejithGCommented:
The code below shows how to add 6 months to current date.

var myDate=new Date();
myDate.setDate(myDate.getMonth()+6);

Please refer javascript Date() object help for more details.
0
Murali MurugesanFull stack Java developerAuthor Commented:
I would be entering the date in a text box as 01/11/2008 (which would be a string value).
 I need to add 12 months to it and show end date as 31/10/2008 .. (please note the same format of dd/mm/yyyy).

-Murali*
0
SreejithGCommented:
Correction in previous code sample. Use setMonth instead of setDate for adding months

var myDate=new Date();
myDate.setMonth(myDate.getMonth()+6);
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

SreejithGCommented:
here is the code what you are looking for

date1 = "01/11/2008"
d1=date1.split("/");
var myDate=new Date(d1[2],d1[1]-1,d1[0]);
myDate.setMonth(myDate.getMonth()+12);
newDate = myDate.getDate() + "/" + (myDate.getMonth()+1) + "/" + myDate.getFullYear()


0
Murali MurugesanFull stack Java developerAuthor Commented:
I just modified ur code as below, but stil i have problems whn the start date is '01/02/2008'..
it gives end date as something like 2017.. and more over if i give 10/01/2008 it gives 31/12/2008 instead of 09/01/2009..

Any suggestions?

-Murali*
<script>
	function calculate(){
		date1 = document.getElementById('frmdt').value;
		d1=date1.split("/");
		var myDate=new Date(d1[2],d1[1]-1,d1[0]);
		myDate.setMonth(myDate.getMonth()+document.getElementById('duration').value);
		var dd = new Date(myDate.getFullYear(), myDate.getMonth(), 0)
		newDate =dd.getDate() + "/" + (dd.getMonth()+1) + "/" + dd.getFullYear();
		document.getElementById('enddt').value = newDate;
	}
 
 
 
 
</script>
 
</HEAD>
 
<BODY>
	<form name="dtForm">
		<INPUT TYPE="text" NAME="frmdt" id="frmdt" value=""/>
		<INPUT TYPE="text" NAME="enddt" id="enddt" value=""/>
 
		<INPUT TYPE="text" NAME="duration" id="duration" value="12"/>
		
		<INPUT TYPE="button" value="Calculate" onclick="calculate();"/>
	</form>
</BODY>
</HTML>

Open in new window

0
Murali MurugesanFull stack Java developerAuthor Commented:
Sry miss typed  0 for day..below is the correct one... it works wrongly when i enter 10/02/2008 as start date..

function calculate(){
            date1 = document.getElementById('frmdt').value;
            d1=date1.split("/");
            var myDate=new Date(d1[2],d1[1]-1,d1[0]-1);
            myDate.setMonth(myDate.getMonth()+document.getElementById('duration').value);
            var dd = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
            newDate =dd.getDate() + "/" + (dd.getMonth()+1) + "/" + dd.getFullYear();
            document.getElementById('enddt').value = newDate;
      }
0
SreejithGCommented:
You have to use parseInt to convert the text from text box to intiger. See the corrected function below.
function calculate()
{
	date1 = document.getElementById('frmdt').value;
	d1=date1.split("/");
	var myDate=new Date(d1[2],d1[1]-1,d1[0]);
	myDate.setMonth(myDate.getMonth()+parseInt(document.getElementById('duration').value,0));
	newDate =myDate.getDate() + "/" + (myDate.getMonth()+1) + "/" + myDate.getFullYear();
	document.getElementById('enddt').value = newDate;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murali MurugesanFull stack Java developerAuthor Commented:
here is my final code for it, it works well for all combinations...

Thanks SreejithG for your help.. i owe you more points than what i mentioned previously.
function calculateDates(arrivalDt){
					var duration = parseInt(document.getElementById('duration').value);
					if(isNaN(duration)){
						alert('Invalid duration');
						return false;
					}
					
					var endDt=arrivalDt.split("/");
					var date = endDt[0];					
					var month = endDt[1];
					var year = endDt[2];
 
					if(parseInt(month) == 2){//feb month
						var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
 
						if (parseInt(date)>29 || (parseInt(date)==29 && !isleap)) {
							alert("February " + year + " doesn't have " + date + " days!");
							return false;	
						}
					}
					
				
					var myDate=new Date(year,month - 1,date - 1);
					myDate.setMonth(myDate.getMonth()+ duration);
					var dd = new Date(myDate.getYear(),myDate.getMonth(), myDate.getDate());
					newDate =append(dd.getDate()) + "/" + append((dd.getMonth()+1)) + "/" + dd.getFullYear();
					return newDate;
				}
				
				function append(x) {return(x<0||x>9?"":"0")+x}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.