March 14, 2013

Duration in terms of Year/Month/Days and Hour/Minute/Second between two dates

This post will show you the duration in terms of Year, Month, Days, Hour, Minute and Second between two dates.

The output of this example is



Let starts coding (Remember this is console application, you may change this code as you feeling better)

C# CODE:
static void Main(string[] args)
{
    DateTime startDate = Convert.ToDateTime("08/01/2012 09:00:00 AM");
    string getDuration = GetDateTimeDurationFromDateDiff(startDate, DateTime.Now);
    Console.WriteLine("Date Difference in terms of Year, Month, Days, Hour, Minute and Second: \n");
    Console.WriteLine(getDuration);
}

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() + "Years ";
    }

    if (month > 0)
    {
        duration += month.ToString() + " Months ";
    }
    if (days > 0)
    {
        duration += days.ToString() + " Days";
    }

    #endregion

    #region Get Hours, Minutes, Seconds

    TimeSpan span = endDate.Subtract(startDate);
    duration +=
        " " + span.Hours + " hours " + span.Minutes + " minutes " + span.Seconds + " seconds ";

    #endregion

    return duration.Trim();            
}

VB.NET CODE:
Private Shared Sub Main(args As String())
    Dim startDate As DateTime = Convert.ToDateTime("08/01/2012 09:00:00 AM")
    Dim getDuration As String = GetDateTimeDurationFromDateDiff(startDate, DateTime.Now)
    Console.WriteLine("Date Difference in terms of Year, Month, Days, Hour, Minute and Second: " & vbLf)
    Console.WriteLine(getDuration)
End Sub

Private Shared Function GetDateTimeDurationFromDateDiff(startDate As DateTime, endDate As DateTime) As String
    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() + "Years "
    End If

    If month > 0 Then
        duration += month.ToString() + " Months "
    End If
    If days > 0 Then
        duration += days.ToString() + " Days"
    End If

    Dim span As TimeSpan = endDate.Subtract(startDate)
    duration += " " + span.Hours + " hours " + span.Minutes + " minutes " + span.Seconds + " seconds "

    Return duration.Trim()
End Function