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?
 
SreejithGConnect With a Mentor Commented:
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
 
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

var myDate=new Date();
myDate.setMonth(myDate.getMonth()+6);
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.