C# Split Times into Hour Blocks
See the question and my original answer on StackOverflowI would do this:
public static IEnumerable<DateTime> GetIntervals(DateTime clockIn, DateTime clockOut)
{
yield return clockIn;
clockIn = clockIn.AddHours(1).Subtract(clockIn.TimeOfDay).AddHours(clockIn.Hour);
for (DateTime dt = clockIn; dt < clockOut; dt = dt.AddHours(1))
yield return dt;
yield return clockOut;
}
Use it like this:
foreach (DateTime dt in GetIntervals(DateTime.Parse("5/25/2011 1:40:56PM", CultureInfo.InvariantCulture), DateTime.Parse("5/25/2011 6:22:12PM", CultureInfo.InvariantCulture)))
{
Console.WriteLine(dt);
}