January 9, 2013

Get the different between two DateTime

You found a lot of code on internet related to DateTime difference in terms of Year, Months and Days and also for Hours, Minutes and Seconds. Here I am showing you my code which is very simple and easy to understand. 

----------------------C#.NET Code :----------------------


-------- METHOD ---------------------

static string GetDateTimeDurationFromDateDiff(DateTime startDate, DateTime endDate)
{
    #region Get Year, Month, Days

    string duration = string.Empty;
    int year = 0, month = 0, days = 0;

    var timeSpan = new TimeSpan();
    timeSpan = endDate.Subtract(startDate);
    year = (timeSpan.Days / 365);

    do
    {
        for (int i = 0; i <= 12; i++)
        {
            if (endDate.Subtract(startDate.AddYears(year).AddMonths(i)).Days > 0)
            {
                month = i;
            }
            else
            {
                break;
            }
        }

        if (month > 12)
        {
            year = year + 1;
        }
    }
    while (month > 12);

    days = endDate.Subtract(startDate.AddYears(year).AddMonths(month)).Days;

    if (year > 0)
    {
        duration += year.ToString() + "Year(s) ";
    }

    if (month > 0)
    {
        duration += month.ToString() + " Month(s) ";
    }
    if (days > 0)
    {
        duration += days.ToString() + " Day(s)";
    }

    #endregion

    #region Get Hours, Minutes, Seconds

    TimeSpan span = endDate.Subtract(startDate);
    duration +=
        " " + span.Hours + " hour(s) " + span.Minutes + " minute(s) " + span.Seconds + " second(s) ";

    #endregion

    return duration.Trim();            
}

-------- METHOD CALLING ---------

DateTime startDate = Convert.ToDateTime("08/01/2012 09:00:00 AM");
string getDuration = GetDateTimeDurationFromDateDiff(startDate, DateTime.Now);
Console.WriteLine(getDuration);

-------- OUTPUT ----------------------

5 month(s) 8 day(s) 6 hour(s) 36 minute(s) 17 second(s)

------------------------VB.NET Code :------------------------


-------- METHOD ---------------------

Private Shared Function GetDateTimeDurationFromDateDiff(startDate As DateTime, endDate As DateTime) As String
    '#Region "Get Year, Month, Days"

    Dim duration As String = String.Empty
    Dim year As Integer = 0, month As Integer = 0, days As Integer = 0

    Dim timeSpan = New TimeSpan()
    timeSpan = endDate.Subtract(startDate)
    year = (timeSpan.Days / 365)

    Do
        For i As Integer = 0 To 12
         If endDate.Subtract(startDate.AddYears(year).AddMonths(i)).Days > 0 Then
          month = i
         Else
          Exit For
         End If
        Next

        If month > 12 Then
         year = year + 1
        End If
    Loop While month > 12

    days = endDate.Subtract(startDate.AddYears(year).AddMonths(month)).Days

    If year > 0 Then
        duration += year.ToString() + "Year(s) "
    End If

    If month > 0 Then
        duration += month.ToString() + " Month(s) "
    End If
    If days > 0 Then
        duration += days.ToString() + " Day(s)"
    End If

    '#End Region

    '#Region "Get Hours, Minutes, Seconds"

    Dim span As TimeSpan = endDate.Subtract(startDate)
    duration += " " + span.Hours + " hour(s) " + span.Minutes + " minute(s) " + span.Seconds + " second(s) "

    '#End Region

    Return duration.Trim()
End Function

-------- METHOD CALLING ---------

Dim startDate As DateTime = Convert.ToDateTime("08/01/2012 09:00:00 AM")
Dim getDuration As String = GetDateTimeDurationFromDateDiff(startDate, DateTime.Now)
Console.WriteLine(getDuration)

-------- OUTPUT ----------------------

5 month(s) 8 day(s) 6 hour(s) 36 minute(s) 17 second(s)