Click or drag to resize

DateTimeExtensionsMonthsBetween Method

Returns the total number of months between two DateTime values.

Namespace: InnerDrive.Core
Assembly: InnerDrive.Core (in InnerDrive.Core.dll) Version: 5.0.8475.0
Syntax
C#
public static int MonthsBetween(
	this DateTime firstDate,
	DateTime secondDate
)

Parameters

firstDate  DateTime
The first DateTime value.
secondDate  DateTime
The second DateTime value.

Return Value

Int32
An Int32 containing the total number of months between firstDate and secondDate.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type DateTime. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks
Only the Month portions of the values are considered.

The parameters firstDate and secondDate may be specified in any order. If firstDate is less than or equal to secondDate, a positive number will be returned. If firstDate is larger, a negative number will be returned.

First dateSecond dateResult
2001-01-012001-03-312
2001-01-312001-03-012
2001-01-012001-01-310
2001-01-312001-01-010
2001-01-012000-12-31-1
Example
C#
var firstDate = new DateTime(2002, 1, 1);
var secondDate = new DateTime(2003, 1, 1);
? firstDate.MonthsBetween(secondDate);

// Result: 12
See Also