para cada día del mes [duplicado]

Posible duplicado:
¿Cómo recorro un rango de fechas?

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))
{
}

preguntado el 01 de febrero de 12 a las 14:02

What you need to to with each day? Do you need a full date or just numeric value for each day? -

I actually think it would be brilliant if DateTime did return an IEnumerable<DateTime> for a requested date range. -

@sll I'm making a calendar where the whole year is visible, and where people would be able to make notes on single dates -

5 Respuestas

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);
    }
}

Entonces llámalo con:

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

This is probably overkill for something you're only doing una vez, 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 solo what you want to achieve, rather than the mechanics for how you're doing it.

Respondido 01 Feb 12, 18:02

Even better replace the first two lines of this function with: foreach(var day in Enumerable.Range(1, DateTime.DaysInMonth(year, month)) - DamienG

I feel like this question just got Skeeted over. - Joe

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

@Aston: Yup, absolutely. - Jon Skeet

Try using a for loop instead.

for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++)
{
  DateTime dt = new DateTime(year, month, i);
}

Respondido 01 Feb 12, 18:02

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));

respondido 04 mar '16, 19:03

Or one step further Enumerable.Range(1, DateTime.DayInMonth(2012, 1)).Select(day => new DateTime(2012, 1, day)) - Ray

@Ray, thanks! Great idea - Joe

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<System.DateTime> does not contain a definition for ForEach and no extension method ForEach of type System.Collections.Generic.IEnumerable<System.DateTime> could be found. Are you missing an assembly reference? (CS1061) - las pruebas

@testing, Probably, need using System.Linq - Joe

IEnumerable<T>.ForEach is not an extension-method from LINQ. The method is provided in the (now old) LINQ Extensions Library. The easiest alternative in BCL would be to use .ToList().ForEach(...) - Markus Jarderot

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)) {
}

Respondido 01 Feb 12, 18:02

I love making use of the often-neglected capability to use for loops with data types other than int. - Calvin Fisher

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))

contestado el 24 de mayo de 12 a las 18:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.