We help IT Professionals succeed at work.

# Subtracting dates to gets the number of Months

on
329 Views
hi there,

Wondered if anyone would have done anything on dates subtracting in order to get the number of months between any 2 dates or any online tutorial on how to do this.
Comment
Watch Question

## View Solution Only

Systems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Could you explain in English words what isthe number of months for you, please.
For example, what is the number of months betwean today and tomorrow? Or betwean today and first of March?
Systems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:

<html>
<script>
function getMonths(theButton){
theForm = theButton.form;
fDate = new Date(theForm.fromDate.value);
tDate = new Date(theForm.toDate.value);
if(fDate>tDate){
return false;
}
mCount = -1;
while(tDate>=fDate){
tDate = new Date(tDate.setMonth(tDate.getMonth()-1));
mCount++;
}
}
</script>
<body>
<form>
DateFormat: <b>MM/DD/YYYY</b><br>
FromDate:
<input type=text name="fromDate">
ToDate:
<input type=text name="toDate">
<input type=button value="Months" onClick="getMonths(this)">
</form>
</body>
</html>

Commented:
as in e.g subtract  dateValue1 =  26/03/2004         dateValue2 =    from 25/12/2005
Systems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Sorry:

Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
mi_cky, The diffrence between dates in terms of months is not defined properly beacuse of the different number off days in each month. How would you define the answer to subtract  26/03/2004   from 25/12/2005. The difference between dates in terms of milli sewcondsseconds  or days etc is easy, eg

<script>
dateStr1='26/03/2004'
dateStr2='25/12/2005'

//form date objects for the given strings (must change to yyyy/mm/dd format)
dArr1=dateStr1.split('/')
dArr2=dateStr2.split('/')
d1=new Date(dArr1[2],dArr1[1]-1,dArr1[0])
d2=new Date(dArr2[2],dArr2[1]-1,dArr2[0])

//calculate difference in millisecs using getTime() method for each date object
diff=d2.getTime()-d1.getTime()

//calculate and output anything you want
</script>
Systems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Thanks.

Commented:
thanks  GwynforWeb,

so to work out months I would have something such as

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.