C # time division into time blocks

I need some help in dividing two hours into hourly intervals between them.

This works with pay data, so it must be very accurate. I need to take clockin and timeout and divide them into hourly intervals.

Example:

clockin = 5/25/2011 1:40:56 PM

clockout = 5/25/2011 6:22:12 PM

I need it to look like this:

5/25/2011 1:40:56 PM

5/25/2011 2:00:00 PM

5/25/2011 3:00:00 PM

5/25/2011 4:00:00 PM

5/25/2011 5:00:00 PM

5/25/2011 6:00:00 PM

5/25/2011 6:22:12 PM

Then I plan to check these times on the "differential" table to see that they should have a new payment code. But later I will worry about the billing code.

Any help splitting time? Prefer C #, but I also have access to MSSQL2000 (where we draw the original times)

+4
source share
4 answers
var hours = new List<DateTime>(); hours.Add(clockin); var next = new DateTime(clockin.Year, clockin.Month, clockin.Day, clockin.Hour, 0, 0, clockin.Kind); while ((next = next.AddHours(1)) < clockout) { hours.Add(next); } hours.Add(clockout); 
+3
source

How about something like that?

 static IEnumerable<DateTime> GetWorkingHourIntervals(DateTime clockIn, DateTime clockOut) { yield return clockIn; DateTime d = new DateTime(clockIn.Year, clockIn.Month, clockIn.Day, clockIn.Hour, 0, 0, clockIn.Kind).AddHours(1); while (d < clockOut) { yield return d; d = d.AddHours(1); } yield return clockOut; } 

In this case, iterator blocks , but can be easily rewritten to return a list instead.

Usage example:

 static void Main(string[] args) { var clockIn = new DateTime(2011, 5, 25, 13, 40, 56); var clockOut = new DateTime(2011, 5, 25, 18, 22, 12); var hours = GetWorkingHourIntervals(clockIn, clockOut); foreach (var h in hours) Console.WriteLine(h); Console.ReadLine(); } 

Output:

  2011-05-25 13:40:56
 2011-05-25 14:00:00
 2011-05-25 15:00:00
 2011-05-25 16:00:00
 2011-05-25 17:00:00
 2011-05-25 18:00:00
 2011-05-25 18:22:12

Update : LukeH was smart enough to suggest that you should copy DateTimeKind as well. This is a really smart move if you plan to convert dates and times to local time later.

+10
source

I think something like this should work:

 public IEnumerable<DateTime> GetHourlyBreakdown(DateTime startDate, DateTime endDate) { var hours = new List<DateTime>(); hours.Add(startDate); var currentDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, 0, 0).AddHours(1); while(currentDate < endDate) { hours.Add(new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, currentDate.Hour, 0, 0)); currentDate = currentDate.AddHours(1); } hours.Add(endDate); return hours; } 
+2
source

I 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 as follows:

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

All Articles