Question

I have a time period of an incident (e.g. 06 April 2014 at 11:30 until 08 April 2014 at 10:15) and I have office opening hours (e.g. 9:00 to 17:00 Monday to Friday).

I need to calculate the number of opening hours (as a decimal) that were affected by the incident.

I can think of a way I can do this but it seems very lengthy and messy so wanted to see if anyone could come up with something elegant?

Thanks Kev

Était-ce utile?

La solution

Here's a short and readable approach using LINQ. Note that it's neither tested sufficiently nor efficient. But maybe it's helpful anyway:

DateTime incidentStart = new DateTime(2014, 04, 06, 11, 30, 0, 0, 0);
DateTime incidentEnd = new DateTime(2014, 04, 08, 10, 15, 0, 0, 0);
int minutes =  (int)(incidentEnd - incidentStart).TotalMinutes;
TimeSpan officeOpen = TimeSpan.FromHours(9);
TimeSpan officeClosed = TimeSpan.FromHours(17);
decimal numHours = Enumerable.Range(0, minutes)
    .Select(min => incidentStart.AddMinutes(min))
    .Where(dt => dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday 
       &&  dt.TimeOfDay >= officeOpen && dt.TimeOfDay < officeClosed)
    .GroupBy(dt => new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, 0)) // round to hour
    .Count();

The result is 11 which seems to be correct, isn't it?

Autres conseils

This answer is accurate to the nearest second.

DateTime startDate = new DateTime(2014, 04, 06, 11, 30, 0, 0, 0);
DateTime endDate = new DateTime(2014, 04, 08, 10, 15, 0, 0, 0);
TimeSpan StartOfDay = new TimeSpan(9, 0, 0);
TimeSpan EndOfDay = new TimeSpan(17, 0, 0);            
int secondCount= 0;

while ((endDate - startDate).Minutes >= 0)
{
    if (startDate.DayOfWeek >= DayOfWeek.Monday && startDate.DayOfWeek <= DayOfWeek.Friday)
        if (startDate.TimeOfDay > StartOfDay && startDate.TimeOfDay <= EndOfDay)                        
            secondCount++;
    startDate = startDate.AddSeconds(1);
}

Console.WriteLine("Total Hours and Minutes: " + new TimeSpan(0, 0, secondCount));

Non-LINQ approach, also as Tims answer, not tested. Use at your own risk

var startDate = new DateTime(2014, 04, 06, 11, 30, 0, 0, 0);
var endDate = new DateTime(2014, 04, 08, 10, 15, 0, 0, 0);

var openStart = new DateTime(2014, 04, 08, 9, 0, 0);
var openEnd = new DateTime(2014, 04, 08, 17, 0, 0);;

var totalAffected = TimeSpan.Zero;
var checkDate = startDate;
while (checkDate < endDate)
{
    if (IsWorkDay(checkDate))
    {
        totalAffected = totalAffected.Add(openEnd.TimeOfDay - openStart.TimeOfDay);    
    }

    checkDate = checkDate.AddDays(1);
}

if (IsWorkDay(checkDate))
{
    if (endDate.TimeOfDay > openEnd.TimeOfDay)
    {
        totalAffected = totalAffected.Add(openEnd.TimeOfDay - openStart.TimeOfDay);
    }
    else
    {
        totalAffected = totalAffected.Add(endDate.TimeOfDay - openStart.TimeOfDay);
    }
}

double numberOfAffectedHours = totalAffected.TotalHours;

private bool IsWorkDay(DateTime day)
{
    return !(day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top