Murali Murugesan
asked on
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*
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*
ASKER
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*
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*
Correction in previous code sample. Use setMonth instead of setDate for adding months
var myDate=new Date();
myDate.setMonth(myDate.get Month()+6) ;
var myDate=new Date();
myDate.setMonth(myDate.get
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.get Month()+12 );
newDate = myDate.getDate() + "/" + (myDate.getMonth()+1) + "/" + myDate.getFullYear()
date1 = "01/11/2008"
d1=date1.split("/");
var myDate=new Date(d1[2],d1[1]-1,d1[0]);
myDate.setMonth(myDate.get
newDate = myDate.getDate() + "/" + (myDate.getMonth()+1) + "/" + myDate.getFullYear()
ASKER
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*
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>
ASKER
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('f rmdt').val ue;
d1=date1.split("/");
var myDate=new Date(d1[2],d1[1]-1,d1[0]-1 );
myDate.setMonth(myDate.get Month()+do cument.get ElementByI d('duratio n').value) ;
var dd = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
newDate =dd.getDate() + "/" + (dd.getMonth()+1) + "/" + dd.getFullYear();
document.getElementById('e nddt').val ue = newDate;
}
function calculate(){
date1 = document.getElementById('f
d1=date1.split("/");
var myDate=new Date(d1[2],d1[1]-1,d1[0]-1
myDate.setMonth(myDate.get
var dd = new Date(myDate.getFullYear(),
newDate =dd.getDate() + "/" + (dd.getMonth()+1) + "/" + dd.getFullYear();
document.getElementById('e
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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}
var myDate=new Date();
myDate.setDate(myDate.getM
Please refer javascript Date() object help for more details.