Solved

Subtracting dates to gets the number of Months(continued)

Posted on 2004-03-26
8
370 Views
Last Modified: 2008-03-17
>Comment from GwynforWeb
 
continued from > http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20933048.html

>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

so how do i accurately work out the amout of months between dates?
diff=d2.getTime()-d1.getTime()

based on this calculation
/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')

0
Comment
Question by:mi_cky
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10687142
What is a month that is the question, this is probably as good as you are going to get

<script>
dateStr2='26/03/2004'
dateStr1='25/12/2005'
dArr1=dateStr1.split('/')
dArr2=dateStr2.split('/')  

months=(dArr1[2]-dArr2[2])*12 + 1*(dArr1[1]-dArr2[1]) + (dArr1[0]-dArr2[0])/28
months=months.toFixed(2)
alert(months)
</script>      
0
 
LVL 7

Expert Comment

by:searlas
ID: 10687236
mi_cky, as mentioned in the referenced question, you need to specify your definition of a month.
today - tomorrow: 0 months, or 1 month?
31/03/2004 - 01/04/2004: 0 months or 1 month?
03/03/2004 - 03/04/2004: clearly 1 month
02/03/2004 - 03/04/2004: 1 month or 2 months?
31/01/2004 - 28/02/2004: 0 months or 1 month?
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 200 total points
ID: 10687665
this is better

months=(dArr1[2]-dArr2[2])*12 + 1*(dArr1[1]-dArr2[1]) + 365*(dArr1[0]-dArr2[0])/12
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 63

Expert Comment

by:Zvonko
ID: 10688705
365.25
0
 

Author Comment

by:mi_cky
ID: 10694749
sorry, wanted to actally award the points to GwynforWeb, :)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10695057
Make a request here and the question can be again unlocked:
http://www.experts-exchange.com/Community_Support/

Also be aware that grading B brings for nobody an advantage.

Cheers,
Zvonko
0
 

Author Comment

by:mi_cky
ID: 10695348
thanks Zvonko
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now