-1

I Need any help to get number of years,months and weeks between 2 dates example From_Date and End_Date

Ian
  • 30,182
  • 19
  • 69
  • 107
ghalib
  • 21
  • 1
  • 8
  • Have you taken a look on `DateTime` `struct` or `NodaTime`? – Ian May 19 '16 at 04:59
  • 2
    check this [link](http://stackoverflow.com/questions/1083955/how-to-get-difference-between-two-dates-in-year-month-week-day?rq=1) – R C May 19 '16 at 05:00
  • already read this article did not seem as perfect solution – ghalib May 19 '16 at 05:03
  • Can you give more details ? What is wrong with other stackoverflow answers on the same question ? what is the expected result ? Can you give an example of what might go wrong with other implementation compared to what you need ? – Zein Makki May 19 '16 at 05:13

3 Answers3

1

C# does not have a library that directly does this, so you would have to come up with your own calculation using the TimeSpan class.

The easiest approach is to add a reference to Microsoft.VisualBasic.dll. Then add a using statement to add the namespace.

using Microsoft.VisualBasic;

Call the static DateDiff function of the DateAndTime class.

Just Specify the dateinterval that you want to get back

var years = DateAndTime.DateDiff(DateInterval.Year, From_Date, End_Date);
var months = DateAndTime.DateDiff(DateInterval.Month, From_Date, End_Date);
var weeks = DateAndTime.DateDiff(DateInterval.Weekday, From_Date, End_Date);
Gregor
  • 196
  • 1
  • 4
1

You can break down the difference to Years, month, weeks ... Specifying your own rules. Here is a simple example:

public Tuple<int, int, int, int> BreakDownDateRange(DateTime fromDate, DateTime toDate)
{
    int years = 0;
    int months = 0;
    int weeks = 0;
    int days = 0;
    DateTime remainingDate = fromDate;
    while (remainingDate.Date < toDate.Date)
    {
        var yearTest = remainingDate.Date.AddYears(1);
        if(yearTest <= toDate.Date)
        {
            years += 1;
            remainingDate = yearTest;
            continue;
        }

        var monthsTest = remainingDate.Date.AddMonths(1);
        if(monthsTest <= toDate.Date)
        {
            months += 1;
            remainingDate = monthsTest;
            continue;
        }

        var weeksTest = remainingDate.Date.AddDays(7);
        if(weeksTest <= toDate.Date)
        {
            weeks += 1;
            remainingDate = weeksTest;
            continue;
        }

        var daysTest = remainingDate.Date.AddDays(1);
        if(daysTest <= toDate.Date)
        {
            days += 1;
            remainingDate = daysTest;
            continue;
        }
    }

    return Tuple.Create(years, months, weeks, days);
}

There is no doubt that this can be optimized, but i can leave that to you.

(If year test fail once, it will fail always, so you can add a boolean for that, and so on for the rest)

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

This is a tricky question. What is a year? and what is a month? Would you count 29 days as 1 month, or 0? Equally, would you count 340 days as a year, or zero years? This is basically why they're missing in the TimeSpan class.

You can easily make your own extension methods to add YOUR logic to this, for example:

public static int FullYears(this TimeSpan timeSpan)
{
    return (int) Math.Ceiling(timeSpan/365);
}

But the construct is heavily depending on your own definitions of what constitutes a year, month and week :)

Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30