Solved

calculate the difference between dates

Posted on 2006-07-18
7
179 Views
Last Modified: 2008-03-17
I need to calculate the difference between dates, as to get the number of months between them.

Please advice.
0
Comment
Question by:sahnias
  • 2
  • 2
7 Comments
 
LVL 19

Expert Comment

by:nschafer
ID: 17131621
Hi sahnias,
Try the following function

  <script type="text/javascript">
    function daysDiff(x,y) {
      return((x - y)/(1000*60*60*24));
    }
   
   var sDate = new Date('01/01/2005')
   var eDate = new Date('01/01/2006')
   alert(daysDiff(eDate,sDate));
  </script>

Hope this helps,
Neal.
0
 
LVL 4

Author Comment

by:sahnias
ID: 17131660
this will give me days difference... and i have that code already... however how do i get the months difference.
0
 
LVL 19

Accepted Solution

by:
nschafer earned 50 total points
ID: 17131749
sahnias,
Sorry about that.  I guess I need to learn to read better.  

This should work for you.

    function monthsDiff(x,y) {
      var smonth = y.getMonth();
      var emonth = x.getMonth();
      var syear = y.getFullYear();
      var eyear = x.getFullYear();
      var months = (eyear - syear) * 12 + emonth - smonth
      if (y.getDate() > x.getDate()) months -=1;
      return months;
    }
   
   var sDate = new Date('03/15/2005')
   var eDate = new Date('01/01/2006')
   alert(monthsDiff(eDate,sDate));


Neal.
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17137432
NO POINTS Please. As I am posting a link to my post.

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21566387.html
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17137444
<SCRIPT LANGUAGE="JavaScript">
function CalculateDiff(d1, d2) {
var date1 = new Date (d1);
var date2 = new Date (d2);
var diff ;
if (date1 > date2) {
diff = date1 - date2;
}
else {
diff = date2 - date1;
}
diff = new Date (diff);

// Difference in months

alert ('Difference in Months : ' + diff.getMonth()+1);

}
CalculateDiff ('01/05/2006', '07/19/2006');
</script>
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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

920 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

17 Experts available now in Live!

Get 1:1 Help Now