35

Possible Duplicate:
How do I loop through a date range?

Is there a way to make a foreach loop for each day in a specific month?

thinking of something like

foreach (DateTime date in DateTime.DaysInMonth(2012, 1))
{
}
Community
  • 1
  • 1
Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89

5 Answers5

75

You can write a helper method pretty easily:

public static IEnumerable<DateTime> AllDatesInMonth(int year, int month)
{
    int days = DateTime.DaysInMonth(year, month);
    for (int day = 1; day <= days; day++)
    {
         yield return new DateTime(year, month, day);
    }
}

Then call it with:

foreach (DateTime date in AllDatesInMonth(2012, 1))

This is probably overkill for something you're only doing once, but it's much nicer than using a for loop or something similar if you're doing this a lot. It makes your code say just what you want to achieve, rather than the mechanics for how you're doing it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Even better replace the first two lines of this function with: foreach(var day in Enumerable.Range(1, DateTime.DaysInMonth(year, month)) – DamienG Feb 01 '12 at 16:18
  • 1
    I feel like this question just got Skeeted over. – Joe Mar 17 '15 at 23:59
  • 1
    You can go one step further if you are using linq by replacing all of the lines inside the function with this: return Enumerable.Range(1, DateTime.DaysInMonth(year, month)).Select(day => new DateTime(year, month, day)); – Aston Oct 11 '18 at 17:30
  • 1
    @Aston: Yup, absolutely. – Jon Skeet Oct 12 '18 at 06:32
25

Try using a for loop instead.

for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++)
{
  DateTime dt = new DateTime(year, month, i);
}
Brandon
  • 68,708
  • 30
  • 194
  • 223
13

You can use Range:

Enumerable
    .Range(1, DateTime.DayInMonth(2012, 1)
    .Select(i => new DateTime(2012, 1, i)))
    .ToList() // ForEach is not a Linq to Sql method (thanks @Markus Jarderot)
    .ForEach(day => Console.Write(day));
Joe
  • 80,724
  • 18
  • 127
  • 145
  • 3
    Or one step further `Enumerable.Range(1, DateTime.DayInMonth(2012, 1)).Select(day => new DateTime(2012, 1, day))` – Ray Feb 01 '12 at 14:07
  • I don't get this working `Enumerable .Range(1, DateTime.DaysInMonth(2012, 1)) .Select(i => new DateTime(2012, 1, i)) .ForEach(day => Console.Write(day));`. *Error CS1061: Type System.Collections.Generic.IEnumerable does not contain a definition for ForEach and no extension method ForEach of type System.Collections.Generic.IEnumerable could be found. Are you missing an assembly reference? (CS1061)* – testing Mar 12 '15 at 10:33
  • @testing, Probably, need `using System.Linq` – Joe Mar 16 '15 at 13:11
  • @Joe: I did this, but the error was still there. Perhaps the Xamarin Linq version is different from the Visual Studio one ... – testing Mar 16 '15 at 13:19
  • 1
    `IEnumerable.ForEach` is not an extension-method from LINQ. The method is provided in the (now old) [LINQ Extensions Library](https://linqlib.codeplex.com/). The easiest alternative in BCL would be to use `.ToList().ForEach(...)` – Markus Jarderot Mar 04 '16 at 16:25
10

You can do it with a simple loop:

DateTime first = new DateTime(2012, 1, 1);
for (DateTime current = first ; current.Month == first.Month ; current = current.AddDays(1)) {
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

It is fairly easy to generate an enumeration of days. Here is one way to do it

Enumerable.Range(1, DateTime.DaysInMonth(year, month)).Select(day =>
    new DateTime(year, month, day))
dplante
  • 2,445
  • 3
  • 21
  • 27
Huusom
  • 5,654
  • 1
  • 17
  • 15