Solved

Subtracting dates to gets the number of Months(continued)

Posted on 2004-03-26
8
372 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is my $_POST not going to results page 10 65
REST call Failing 1 37
Multiple of Image Swaps 5 40
How to confirm and grey the submit button using JavaScript ? 14 61
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

738 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