4

Below is my code. I am only getting the difference between two dates, but I want the name of that month which comes between the from and to dates.

public static int GetMonthsBetween(DateTime from, DateTime to)
{
    if (from > to) return GetMonthsBetween(to, from);

    var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));

    if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
    {
        return monthDiff - 1;
    }
    else
    {
        return monthDiff;
    }
}
Andre
  • 1,228
  • 11
  • 24
Sanjana
  • 43
  • 1
  • 7
  • Would you retag this? I think it is Java, but am not sure. Did you mean to use the `na` tag? – halfer Sep 23 '15 at 08:44
  • 1
    @halfer: It's C# as far as i can tell now ... at least this would explain the ">" operator for comparison. – Aron_dc Sep 23 '15 at 08:57
  • Do you need the name from the int based month or do you need the calculation or even the month between both, from and to DateTime? – Andre Sep 23 '15 at 09:18
  • I want name of months that comes between from and to date – Sanjana Sep 23 '15 at 09:39
  • Hi @Sanjana if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Andre Oct 19 '15 at 09:14

3 Answers3

10

Based on your code you could substract the month difference from the "to" DateTime to get DateTime difference from your input.

public static List<DateTime> GetMonthsBetween(DateTime from, DateTime to)
{
    if (from > to) return GetMonthsBetween(to, from);

    var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));

    if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
    {
        monthDiff -= 1;
    }

    List<DateTime> results = new List<DateTime>();
    for (int i = monthDiff; i >= 1; i--)
    {
        results.Add(to.AddMonths(-i));
    }

    return results;
}

To get the name of the month just format the DateTime to "MMM".

var dts = GetMonthsBetween(DateTime.Today, DateTime.Today.AddMonths(5));
foreach (var dateTime in dts)
{
    Console.WriteLine(dateTime.ToString("MMM"));
}
Andre
  • 1,228
  • 11
  • 24
  • your ans is right but it is giving only one month in return and i want list of months that comes between from and to date – Sanjana Sep 23 '15 at 10:00
  • I get the months Mar, Apr, May, Jun for the given dates 2017-03-01 and 2017-07-01. My question is that how can I include the last 7th month(July) too? – Elham Kohestani Aug 28 '17 at 12:35
  • For example call the Method `GetMonthsBetween(from, to.AddMonths(1));` – Andre Aug 29 '17 at 12:04
3

If you want the names of all months between two dates, use something like this:

var d1 = new DateTime(2015,6,1);
var d2 = new DateTime(2015,9,1);

var monthlist = new List<string>();
string format = d1.Year == d2.Year ? "MMMM" : "MMMM yyyy";

for (var d = d1; d <= d2; d = d.AddMonths(1))
{
    monthlist.Add(d.ToString(format));
}

The full list is now in monthlist - you will want to return that from your method.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
1

Assuming you're using Java and JodaTime there are several flaws in your code.

  1. You cant use from > to to evaluate if a date is after an other. Use from.isAfter(to) instead.
  2. JodaTime already supplies a method to calculate the amount of whole months between two given Dates Months.monthsBetween(start,end).
  3. With the calculated month difference you can instantiate a new DateTime object that holds a date in your desired month and output its name via yourNewDateTimeObject.month().getAsText().

edit: Just found out you're using C# so ignore my text above this. Below here I will try to answer your question in C#.

  1. Why dont you just subtract the from from the to date and obtain your difference?

  2. The resulting TimeSpan can be used to determine the amount of whole months between your two given dates.

  3. To obtain the resulting month name you could use yourDateTime.ToString("MMMM");
Aron_dc
  • 883
  • 4
  • 14
  • I don't think it's that easy, TimeSpan has no TotalMonths since a month can be variable amount of days. – Andre Sep 23 '15 at 09:30
  • @Andre I didn't want to program it for him completely as i suppose he is able to do that by himself. I rather wanted to show an alternative that uses the given classes more efficiently (if implemented properly) than writing everything on his own. Additionaly he could use the NodaTime library. – Aron_dc Sep 23 '15 at 10:29