Skip to content

Instantly share code, notes, and snippets.

@jagsbyteinception
Last active July 23, 2021 16:00
Show Gist options
  • Save jagsbyteinception/d80456c4dea4398e9bac3f3576672d8f to your computer and use it in GitHub Desktop.
Save jagsbyteinception/d80456c4dea4398e9bac3f3576672d8f to your computer and use it in GitHub Desktop.
find business hours between 2 datetime exclude option sat, sun and holidays. pass business hours
public static double CalculateTimeDifference(DateTime startDate, DateTime endDate, Boolean excludeSat, Boolean excludeSun, List<DateTime> excludeDates, string businessStartTime, string businessEndTime)
{
//find working hours in a day
var tsBusinessStartTime = TimeSpan.Parse(businessStartTime);
var tsBusinessEndTime = TimeSpan.Parse(businessEndTime);
var businessHours = (tsBusinessEndTime - tsBusinessStartTime).TotalHours;
Console.WriteLine(businessHours);
if (startDate.TimeOfDay < tsBusinessStartTime) //email received before business hr
{
startDate = startDate.Add(tsBusinessStartTime - startDate.TimeOfDay); //push received time ahead to "business start time"
Console.WriteLine("pushed time ahead " + startDate);
}
if (endDate.TimeOfDay > tsBusinessEndTime) //reply sent after business hr
{
endDate.Subtract(endDate.TimeOfDay - tsBusinessEndTime); //push reply time back to "business end time"
Console.WriteLine("pushed time back " + endDate);
}
if (startDate.Date == endDate.Date)
{
return (endDate - startDate).TotalHours;
}
else
{
double fullDayHours = 0;
for (var i = startDate.Date.AddDays(1); i < endDate.Date; i = i.AddDays(1))
{
//Console.WriteLine(i.DayOfWeek);
if ((excludeSat && i.DayOfWeek == DayOfWeek.Saturday) || (excludeSun && i.DayOfWeek == DayOfWeek.Sunday) || excludeDates.Contains(i.Date))
{
//skip day
}
else
{
fullDayHours += businessHours;
}
}
var startDateHours = (tsBusinessEndTime - startDate.TimeOfDay).TotalHours;
var endDateHours = (endDate.TimeOfDay - tsBusinessStartTime).TotalHours;
Console.WriteLine(" f " + fullDayHours + " s " + startDateHours + " e " + endDateHours);
return fullDayHours + startDateHours + endDateHours;
}
}
@jagsbyteinception
Copy link
Author

`
public static double CalculateTimeDifference(DateTime startDate, DateTime endDate, Boolean excludeSat, Boolean excludeSun, List excludeDates, string businessStartTime, string businessEndTime)
{
//find working hours in a day
var tsBusinessStartTime = TimeSpan.Parse(businessStartTime);
var tsBusinessEndTime = TimeSpan.Parse(businessEndTime);
var businessHours = (tsBusinessEndTime - tsBusinessStartTime).TotalHours;
var excludeStartDay = ExcludeDay(startDate, excludeSat, excludeSun, excludeDates);
var excludeEndDay = ExcludeDay(endDate, excludeSat, excludeSun, excludeDates);

Console.WriteLine(businessHours + " ExStart " + excludeStartDay + " ExEnd " + excludeEndDay);

if (startDate.TimeOfDay < tsBusinessStartTime) //email received before business hr
{
  startDate = startDate.Add(tsBusinessStartTime - startDate.TimeOfDay); //push received time ahead to "business start time"
  Console.WriteLine("pushed time ahead " + startDate);
}
else if (startDate.TimeOfDay > tsBusinessEndTime) //email received before business hr
{
  startDate = startDate.Subtract(startDate.TimeOfDay - tsBusinessEndTime); //push received time ahead to "business start time"
  Console.WriteLine("pushed time back " + startDate);
}

//if (endDate.TimeOfDay > tsBusinessEndTime) //reply sent after business hr
//{
//  endDate.Subtract(endDate.TimeOfDay - tsBusinessEndTime); //push reply time back to "business end time"
//  Console.WriteLine("pushed time back " + endDate);
//}

if (startDate.Date == endDate.Date)
{
  var hours = excludeStartDay ? 0 : (endDate - startDate).TotalHours;
  return hours < 0 ? 0 : hours;
}
else
{
  double fullDayHours = 0;
  for (var i = startDate.Date.AddDays(1); i < endDate.Date; i = i.AddDays(1))
  {
    //Console.WriteLine(i.DayOfWeek);
    if (ExcludeDay(i, excludeSat, excludeSun, excludeDates))
    {
      //skip day
    }
    else
    {
      fullDayHours += businessHours;
    }
  }

  var startDateHours = excludeStartDay ? 0 : (tsBusinessEndTime - startDate.TimeOfDay).TotalHours;
  var endDateHours = excludeEndDay ? 0 : (endDate.TimeOfDay - tsBusinessStartTime).TotalHours;
  if (endDateHours < 0) //replied before business day started
  {
    endDateHours = 0;
  }
  if (startDateHours < 0)
  {
    startDateHours = 0;
  }
  Console.WriteLine(" f " + fullDayHours + " s " + startDateHours + " e " + endDateHours);
  return fullDayHours + startDateHours + endDateHours;
}

}

public static bool ExcludeDay(DateTime day, Boolean excludeSat, Boolean excludeSun, List excludeDates)
{
return (excludeSat && day.DayOfWeek == DayOfWeek.Saturday) || (excludeSun && day.DayOfWeek == DayOfWeek.Sunday) || excludeDates.Contains(day.Date);
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment