Last active
July 23, 2021 16:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`
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);
}
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);
}
`