Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

month b/w 2 dates (inclusive)

Status
Not open for further replies.

meinhunna

Programmer
Jul 31, 2004
118
0
0
AU
how can i find number of months between two dates (inclusive)

i.e including start date and end date

for eg

01/01/2004 and 20/12/2004 ==> 12

thanks


 
I've created a function. Maybe is not perfect but it works.
Sory because the code is in C#.
Code:
		public static int DateTimeMonthsSpan(DateTime DateTime1, DateTime DateTime2)
		{
			int i = 0;
			int sign;
			DateTime1 = DateTime1.AddDays(1 - DateTime1.Day);
			DateTime2 = DateTime2.AddDays(1 - DateTime2.Day);

			if (DateTime1 == DateTime2)
				return 0;

			
			if (DateTime1.Year > DateTime2.Year)
				sign = 1;
			else if (DateTime1.Year == DateTime2.Year)
				if (DateTime1.Month > DateTime2.Month)
					sign = 1;
				else
					sign = -1;
			else 
				sign = -1;

			do
			{
				i++;
				DateTime2 = DateTime2.AddMonths(sign);
			}
			while(DateTime1 != DateTime2);
			return i * sign;
		}
 
I think this works as well

Code:
DateDiff(DateInterval.Month, #1/1/2004#, #12/20/2004#) +1

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top