Solved

# Javascript date function to calculate the end date using the duration

Posted on 2008-11-04
Medium Priority
1,687 Views
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*
0
Question by:Murali Murugesan
• 4
• 4

LVL 3

Expert Comment

ID: 22883026
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

LVL 13

Author Comment

ID: 22883042
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

LVL 3

Expert Comment

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

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

LVL 3

Expert Comment

ID: 22883067
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

LVL 13

Author Comment

ID: 22883500
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>

<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>
``````
0

LVL 13

Author Comment

ID: 22883514
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

LVL 3

Accepted Solution

SreejithG earned 2000 total points
ID: 22883548
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;
}
``````
0

LVL 13

Author Comment

ID: 22883835
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)){
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}
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month15 days, 8 hours left to enroll