473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Months Difference

How can we find months difference between two dates.

For Exp :

Start Date : 05/03/2007
End Date : 06/10/2007

How to calucate months difference for any dates?.
May 3 '07 #1
3 2069
vituko
48
Expand|Select|Wrap|Line Numbers
  1. var a = new Date (2000, 1, 1) ;
  2. var b = new Date (2002, 5, 1) ;
  3. alert (12 * (b.getYear() - a.getYear()) + b.getMonth() - a.getMonth()) ;
  4.  
May 3 '07 #2
Expand|Select|Wrap|Line Numbers
  1. private int CalculateMonthDifference(DateTime startDate, DateTime endDate)
  2.         {
  3.             int diff = 0;
  4.  
  5.             while(startDate <= endDate)
  6.             {
  7.                 endDate = endDate.AddMonths(-1);
  8.                 diff++;
  9.             }
  10.             if (diff == 0)
  11.             {
  12.                 return diff;
  13.             }
  14.             else
  15.             {
  16.                 return diff -1;
  17.             }
  18.         }
This will loop through and give the number of months difference by subtracting 1 month from the end date until it less than the start date. Ok its not elegent but it works and the number of loops is not massive (e.g. 10 years diff would be 1200 loops which on todays machines is nothing)
Jul 27 '07 #3
gits
5,390 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. private int CalculateMonthDifference(DateTime startDate, DateTime endDate)
  2.         {
  3.             int diff = 0;
  4.  
  5.             while(startDate <= endDate)
  6.             {
  7.                 endDate = endDate.AddMonths(-1);
  8.                 diff++;
  9.             }
  10.             if (diff == 0)
  11.             {
  12.                 return diff;
  13.             }
  14.             else
  15.             {
  16.                 return diff -1;
  17.             }
  18.         }
This will loop through and give the number of months difference by subtracting 1 month from the end date until it less than the start date. Ok its not elegent but it works and the number of loops is not massive (e.g. 10 years diff would be 1200 loops which on todays machines is nothing)
hi ...

unfortunatly this is not a javascript solution to the problem?

kind regards
Jul 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Fabian | last post by:
Is there any way of determining when the first day of a *lunar* month (far eastern lunar months if it makes a difference) falls, using javascript? -- -- Fabian Visit my website often and...
3
by: Joseph S | last post by:
I am having problems with the following countdown script. For some reason, the number of months is occasionally wrong. I can't quite put my finger on the problem. Any assistance or...
7
by: bryanilton | last post by:
I was hoping someone could help me with this.. I'm not very familiar with javascript but I'm pretty sure it can help me with what I need to accomplish. I've tried piecing several pieces of script...
2
by: lpeterson | last post by:
I'm trying to calculate the number of months between dates that begin in 2003 and end in 2004(ex. 1/2/2003 - 7/2/2004). I need the total months in 2003 and in 2004 both of which should add up to...
5
by: Juan | last post by:
Hi everyone, is there a function that calculates the exact amount of Years, Months, and Days between two days? TimeDiff is not good enough, since it calculates, for example: Date 1: Dec....
5
by: wespw1 | last post by:
I have a form where I have 2 dates: 06/27/2007 01/28/1996 What I need to get is the difference between the 2 dates in months. Or in otherwords I am trying to get the age of the 2 dates in...
1
by: student2007 | last post by:
Greetings to everyone, I have the following code to find a date at the first of the month following a time period. Now I need to find the difference in years and months from this new date to...
2
by: Shamim1983 | last post by:
I want to find difference between 2 dates in number of months using GregorianCalendar utility. I want it to work fine considering the year having 28, 29, 30 or 31 days in month. It should give me...
1
by: =?Utf-8?B?Q2hyaXNiZDE=?= | last post by:
I need to track to the difference, in days and months between two date-a date in the past and today in Excel 2003.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.