14

I am trying to make a function which gives all month name between two dates in c#

List<string> liMonths = MyFunction(date1,date2);

my function is

MyFunction(DateTime date1,DateTime date2)
{

//some code
return listOfMonths;
}

can you help me how could i do this

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83

7 Answers7

36

No linq-solution yet?

var start = new DateTime(2013, 1, 1);
var end = new DateTime(2013, 6, 22);

// set end-date to end of month
end = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month));

var diff = Enumerable.Range(0, Int32.MaxValue)
                     .Select(e => start.AddMonths(e))
                     .TakeWhile(e => e <= end)
                     .Select(e => e.ToString("MMMM"));
sloth
  • 99,095
  • 21
  • 171
  • 219
6

You can use Linq with a helper function

IEnumerable<DateTime> GetDates(DateTime date1, DateTime date2) 
{
    while (date1 <= date2) 
    {
        yield return date1;
        date1 = date1.AddMonths(1);
    }

    if (date1 > date2) 
    { 
        // Include the last month
        yield return date1;
    }
}

Then MyFunction can be one of the following

1) Include year name

List<string> MyFunction(DateTime date1, DateTime date2) {
    return GetDates(date1,date2).Select(x => x.ToString("MMMM yyyyy")).ToList();
}

2) Just the month name, with duplicates

List<string> MyFunction(DateTime date1, DateTime date2) {
  return GetDates(date1,date2).Select(x => x.ToString("MMMM")).ToList();
}

3) Distinct month names

List<string> MyFunction(DateTime date1, DateTime date2) {
    return GetDates(date1,date2).Select(x => x.ToString("MMMM")).Distinct().ToList();
}
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
5

Create a loop starts date1 until date2. Add one month for every step of your loop and fill in your return variable the month.

I try to write in meta language your aim:

DateTime currDate = date1
List myList = new List();
while (currDate < date2) {
    myList.add(getMonthName(currDate));
    currDate = currDate.addMonth(1);
}
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • I used you code and realized that `addMonth(1)` doesn't change the value of currDate, you will have to assign `currDate.addMonth(1)` to a new variable and reassign that variable to `currDate` so as to change its value. Simply do this `while (currDate < date2) { mylist.Add(getMonthName(currDate)); var newDate = currDate.AddMonths(1); currDate= newDate; }` – Dev Nov 05 '15 at 10:38
  • 1
    Hi, I've fixed my answer. with currDate = currDate.addMonth(1); – Joe Taras Nov 05 '15 at 11:35
2
DateTime from;
DateTime to;


var source = from.Month > to.Month ?
               Enumerable.Range(from, 12).Concat(Enumerable.Range(1, to.Month))
             : Enumerable.Range(1, 12)
               .SkipWhile(m => m >= from.Month)
               .TakeWhile(m => m <= to.Month)
var monthes = source
     .Select(m => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m));
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
1
    List<string> MonthNames(DateTime date1, DateTime date2)
    {
        var monthList = new List<string>();

        while (date1 < date2)
        {
            monthList.Add(date1.ToString("MMMM/yyyy"));
            date1 = date1.AddMonths(1);
        }

        return monthList;
    }
paul
  • 21,653
  • 1
  • 53
  • 54
1
static IEnumerable<DateTime> monthsBetween(DateTime d0, DateTime d1)
{
    return Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
                     .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m));
}
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
1
private static List<string> MyFunction(DateTime date1, DateTime date2)
        {

            var listOfMonths=new List<string>();
            if (date1.CompareTo(date2) == 1)
            {
                var temp = date2;
                date2 = date1;
                date1 = temp;
            }

            var mosSt = date1.Month;
            var mosEn = date2.Month;
            var yearSt = date1.Year;
            var yearEn = date2.Year;

            while (mosSt < mosEn || yearSt < yearEn)
            {
                var temp = new DateTime(yearSt, mosSt, 1);
                listOfMonths.Add(temp.ToString("MMMM", CultureInfo.InvariantCulture));
                mosSt++;
                if (mosSt < 13) continue;
                yearSt++;
                mosSt = 1;
            }

            return listOfMonths;
        }
Alyafey
  • 1,455
  • 3
  • 15
  • 23