Solved

Subtracting dates to gets the number of Months(continued)

Posted on 2004-03-26
8
368 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

759 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

19 Experts available now in Live!

Get 1:1 Help Now