We help IT Professionals succeed at work.

Subtracting dates to gets the number of Months

mi_cky
mi_cky asked
on
329 Views
Last Modified: 2012-06-08
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

ZvonkoSystems 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?
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
What about this one:

<html>
<head>
<script>
function getMonths(theButton){
  theForm = theButton.form;
  fDate = new Date(theForm.fromDate.value);
  tDate = new Date(theForm.toDate.value);
  if(fDate>tDate){
    alert("FromDate greater then ToDate.");
    return false;
  }
  mCount = -1;
  while(tDate>=fDate){
    tDate = new Date(tDate.setMonth(tDate.getMonth()-1));
    mCount++;
  }
  alert("Mounts: "+mCount);
}
</script>
</head>
<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>

Author

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

Commented:
Sorry:

 alert("Months: "+mCount);


Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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
alert(diff+' milli secs')  
alert(diff/1000+' secs')
alert(diff/(1000*60)+' mins')  
alert(diff/(1000*60*60)+' hours')
alert(diff/(1000*60*60*24)+' days')
</script>      
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Thanks.

Author

Commented:
thanks  GwynforWeb,

so to work out months I would have something such as
alert(diff/1000*60*60*24 * noofdaysinMonths)

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

Get Access
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

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

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

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.