Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Duration between two DateTimes in minutes

Tags:

I need to determine duration between two DateTimes in minutes.

However, there is a slight twist:

  • exclude weekends
  • only count minutes which are between 7:00AM and 7:00PM. for example: [09/30/2010 6:39:00 PM] - [09/30/2010 7:39:00 PM] = 21 Minutes

I'm just having a hard time coming up with a decent way to do it and would appreciate if anyone can suggest something.

Thanks.


Edit:

I ended up going with dtb's solution. There is only one special case which needed to be taken care of: if end time is after 7:00PM, count the minutes from 7:00AM to the actual end time.

This is how I modified it:

var minutes = from day in start.DaysInRangeUntil(end)                 where !day.IsWeekendDay()                 let st = Helpers.Max(day.AddHours(7), start)                 let en = (day.DayOfYear == end.DayOfYear ?                              end :                             Helpers.Min(day.AddHours(19), end)                             )                 select (en - st).TotalMinutes; 

Again, thanks for the help.

like image 308
Nasir Avatar asked Sep 30 '10 21:09

Nasir


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

You can, of course, use LINQ:

DateTime a = new DateTime(2010, 10, 30, 21, 58, 29); DateTime b = a + new TimeSpan(12, 5, 54, 24, 623);  var minutes = from day in a.DaysInRangeUntil(b)               where !day.IsWeekendDay()               let start = Max(day.AddHours( 7), a)               let end   = Min(day.AddHours(19), b)               select (end - start).TotalMinutes;  var result = minutes.Sum();  // result == 6292.89 

(Note: You probably need to check for a lot of corner cases which I completely ignored.)

Helper methods:

static IEnumerable<DateTime> DaysInRangeUntil(this DateTime start, DateTime end) {     return Enumerable.Range(0, 1 + (int)(end.Date - start.Date).TotalDays)                      .Select(dt => start.Date.AddDays(dt)); }  static bool IsWeekendDay(this DateTime dt) {     return dt.DayOfWeek == DayOfWeek.Saturday         || dt.DayOfWeek == DayOfWeek.Sunday; }  static DateTime Max(DateTime a, DateTime b) {     return new DateTime(Math.Max(a.Ticks, b.Ticks)); }  static DateTime Min(DateTime a, DateTime b) {     return new DateTime(Math.Min(a.Ticks, b.Ticks)); } 
like image 97
dtb Avatar answered Jun 04 '23 15:06

dtb


Take your start time, get the amount of minutes to the end of that day (Ie the 7pm).

Then from the 7am the next day, count the amount of days to the final day (excluding any time into the end day).

Calculate how many (If any) weekends have passed. (For every weekends reduce the count of days by 2).

Do some simple math from there to get the total minutes for the count of days.

Add the extra time on the final day and the start days extra time.

like image 32
Psytronic Avatar answered Jun 04 '23 14:06

Psytronic