Date difference in English


Near duplicates

How do I calculate relative time?

How do I calculate someone’s age in C#?

Anyone know how of a function in VB.NET or C# that will take two dates, calculate the difference and output that difference in an english string? For example if I pass the dates '3/10/2009' and '3/25/2009', the function would return the string "15 Days" or if I pass the dates '3/10/2005' and '3/15/2007', the function would return "2 Years, 5 days"

Use the DateDiff function in VB.NET

Here's some code that will give you years, months and days:

    Dim Date1 = Date.Parse("1/10/2008")
    Dim Date2 = Date.Parse("3/25/2009")
    Dim Years = DateDiff(DateInterval.Year, Date1, Date2)
    If Years > 0 Then
        Date1 = Date1.AddYears(Years)
    End If
    Dim Months = DateDiff(DateInterval.Month, Date1, Date2)
    If Months > 0 Then
        Date1 = Date1.AddMonths(Months)
    End If
    Dim Days = DateDiff(DateInterval.Day, Date1, Date2)
    Console.WriteLine("{0} years, {1} months, {2} days", Years, Months, Days)

Output: 1 years, 2 months, 15 days

You could add a bit more logic to conditionally show each span only if it's not zero and also to use year or years, month or months and day or days, but this code should get you headed in the right direction.