Solved

Subtracting dates to gets the number of Months(continued)

Posted on 2004-03-26
8
369 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

912 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

23 Experts available now in Live!

Get 1:1 Help Now