Solved

Subtracting dates to gets the number of Months(continued)

Posted on 2004-03-26
8
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

688 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