473,799 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Months Difference

14 New Member
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 2110
vituko
48 New Member
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
weedweaver
2 New Member
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 Recognized Expert Moderator Expert
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
3277
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 for long periods! http://www.lajzar.co.uk
3
1419
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 recommendation from someone more knowlegable than me would be appreciated. Thanks much. <script> ty=2005;to=08;td=31;th=24;tm=0;ts=0; function e(){ ny=0;no=0;nd=0;nh=0;nm=0;ns=0;n=new Date();
7
2336
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 together but I've had no luck so far. What I need to do is have a user input for Year, Month and day (preferably drop down) and compare the input to today's date and display the difference in months. Any help with this would be appreciated.
2
2469
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 the length of the date range. As in the example above the total month equals 18, but Access gives me 11 for 2003 and 6 for 2004 which equals 17.. The problem is Access doesn't appear to be counting the month from 12/13/2003 to 1/13/2004 in the...
5
8837
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. 31st 2003 Date 2: Jan 1st 2004
5
1590
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 months. Can anyone help me? Its been a while since I did this in on a form in Access 2003 and it might be simple and I am just probably missing something stupid.
1
3697
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 sysdate(). I have tried to use datediff() to no avail on the new date and even if I got it to work it would only give the days which could be broken down into years but I don't think I could get an accurate picture of months. Tried date_sub but couldn't...
2
2002
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 exact difference in all possible cases. Please help soon. Thanks in advance.
1
1341
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
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.