Showing posts with label Date difference in years. Show all posts
Showing posts with label Date difference in years. Show all posts

Sunday, October 26, 2008

Calculate date difference in years, months and days using VB.NET and C#

Recently I needed function that will return the date difference in years, months and days. I didn't find many posts on internet for accured messuring the difference including the leap years, so this is the class I wrote and I hope you will find it useful.

VB code:


Public Class DateSpan
Public Sub New(ByVal startDate As DateTime, ByVal enddate As DateTime)
_years = 0
_months = 0
_days = 0
Dim ds As DateSpan = DateDiff(startDate, enddate)
If Not ds Is Nothing Then
_years = ds.Years
_months = ds.Months
_days = ds.Days
End If
End Sub
Public Sub New()
_years = 0
_months = 0
_days = 0
End Sub

Private _years As Integer
Private _months As Integer
Private _days As Integer

Public Property Years() As Integer
Get
Return _years
End Get
Set(ByVal value As Integer)
_years = value
End Set
End Property
Public Property Months() As Integer
Get
Return _months
End Get
Set(ByVal value As Integer)
_months = value
End Set
End Property
Public Property Days() As Integer
Get
Return _days
End Get
Set(ByVal value As Integer)
_days = value
End Set
End Property

Overrides Function ToString() As String
Return String.Format("{0} year(s) {1} month(s) {2} day(s)", _years, _months, _days)
End Function
Public Sub Reverse(ByVal reverse As Boolean)
If reverse Then
_years = -1 * _years
_months = -1 * _months
_days = -1 * _days
End If
End Sub

Public Shared Function DateDiff(ByVal startDate As DateTime, ByVal endDate As DateTime) As DateSpan
If (startDate = DateTime.MinValue) OrElse (endDate = DateTime.MinValue) Then Return Nothing
Dim reverse As Boolean = False
' reverse the dates
If startDate > endDate Then
Dim tmpDate As DateTime = endDate
endDate = startDate
startDate = tmpDate
reverse = True
End If

Dim ds As New DateSpan
'
' calculate years
If startDate.Year = endDate.Year Then
ds.Years = 0
Else
ds.Years = endDate.Year - startDate.Year
End If
'
' calculate months
If startDate.Month = endDate.Month Then
ds.Months = 0
ElseIf startDate.Month < months =" endDate.Month" months =" 12" day =" endDate.Day" days =" 0" days =" endDate.Day" days =" Date.DaysInMonth(startDate.Year,">= Date.DaysInMonth(endDate.Year, endDate.Month) AndAlso _
startDate.Day > Date.DaysInMonth(endDate.Year, endDate.Month) Then

ds.Days = ds.Days - Date.DaysInMonth(endDate.Year, endDate.Month)
Else
If ds.Months = 0 Then
ds.Years -= 1
ds.Months = 11
Else
ds.Months -= 1
End If
End If
End If
'
' reverse date (show negative difference)
ds.Reverse(reverse)
Return ds
End Function



End Class


C# code:


public class DateSpan
{

private int _years = 0;
private int _months = 0;
private int _days = 0;

public int Years
{
get { return _years; }
set { _years = value; }
}
public int Months
{
get { return _months; }
set { _months = value; }
}
public int Days
{
get { return _days; }
set { _days = value; }
}

public override string ToString()
{
return string.Format("{0:00}year(s) {1:00} month(s) {2:00} day(s)", _years, _months, _days);
}
public void Reverse(bool reverse)
{
if (reverse)
{
_years = -1 * _years;
_months = -1 * _months;
_days = -1 * _days;
}
}

public static DateSpan DateDiff(DateTime startDate, DateTime endDate)
{
if ((startDate == null) (endDate == null)) return null;
bool reverse = false;
// reverse the dates
if (startDate > endDate)
{
DateTime tmpDate = endDate;
endDate = startDate;
startDate = tmpDate;
reverse = true;
}

DateSpan ds = new DateSpan();
//
// calculate years
if (startDate.Year == endDate.Year)
{
ds.Years = 0;
}
else
{
ds.Years = endDate.Year - startDate.Year;
}
//
// calculate months
if (startDate.Month == endDate.Month)
{
ds.Months = 0;
}
else if (startDate.Month < months =" endDate.Month" months =" 12" day ="=" days =" 0;" days =" endDate.Day" days =" System.DateTime.DaysInMonth(startDate.Year,">= System.DateTime.DaysInMonth(endDate.Year, endDate.Month) && startDate.Day > System.DateTime.DaysInMonth(endDate.Year, endDate.Month))
{

ds.Days = ds.Days - System.DateTime.DaysInMonth(endDate.Year, endDate.Month);
}
else
{
if (ds.Months > 0)
{
ds.Months -= 1;
} else {
ds.Months = 11;
ds.Years -= 1;
}
}
}
//
// reverse date (show negative difference)
ds.Reverse(reverse);
return ds;
}
}