如何从两个日期之间的差异中删除特定时间段
How to delete a particular time slot, from the difference between two dates
string StartDateString = "2020-04-20 18:05:07.6187";
DateTime StartDate = DateTime.Parse(StartDateString);
string EndDateString = "2020-04-22 14:10:00.6187";
DateTime EndDate = DateTime.Parse(EndDateString);
TimeSpan DifferenceStartDateEndDate = StartDate - EndDate;
现在我想从 DifferenceStartDateEndDate(1 天 20 小时 4 分钟)中删除晚上 10 点到早上 8 点之间的时间。 IE。我想删除从 StartDate 到 EndDate 的晚上 10 点到早上 8 点之间的时间。
据我了解,您只需将一天中业务活跃的小时数添加到您的计算中。 (08:00 - 22:00)
这里有两个帮手:
// For the first day in our calculation
// Start at 08:00 if the given date has an hour component that is earlier
static TimeSpan GetTill2200(DateTime date)
{
if (date.Hour >= 22) return TimeSpan.Zero;
if (date.Hour < 8) date = date.Date.AddHours(8);
return date.Date.AddHours(22) - date;
}
// For the last day in our calculation
// End at 22:00 if the given date has an hour component that is later
static TimeSpan GetFrom0800(DateTime date)
{
if (date.Hour < 8) return TimeSpan.Zero;
if (date.Hour >= 22) date = date.Date.AddHours(22);
return date - date.Date.AddHours(8);
}
这里是合并开始日期和结束日期以及中间日期的代码流
// StartDate and EndDate variables as in your question.
TimeSpan result = GetTill2200(StartDate);
DateTime currentDate = StartDate.AddDays(1);
while (currentDate.Date < EndDate.Date)
{
// Add 14 hours
// Total: 17:54:52:3813
result = result.Add(TimeSpan.FromHours(14));
currentDate = currentDate.AddDays(1);
}
// returns 06:10:00.6187
// Total = 1.00:04:53 ( 1 days, 4 minutes, 53 seconds )
result = result.Add(GetFrom0800(EndDate));
// Prints 1.00:04:53
Console.WriteLine(result);
string StartDateString = "2020-04-20 18:05:07.6187";
DateTime StartDate = DateTime.Parse(StartDateString);
string EndDateString = "2020-04-22 14:10:00.6187";
DateTime EndDate = DateTime.Parse(EndDateString);
TimeSpan DifferenceStartDateEndDate = StartDate - EndDate;
现在我想从 DifferenceStartDateEndDate(1 天 20 小时 4 分钟)中删除晚上 10 点到早上 8 点之间的时间。 IE。我想删除从 StartDate 到 EndDate 的晚上 10 点到早上 8 点之间的时间。
据我了解,您只需将一天中业务活跃的小时数添加到您的计算中。 (08:00 - 22:00)
这里有两个帮手:
// For the first day in our calculation
// Start at 08:00 if the given date has an hour component that is earlier
static TimeSpan GetTill2200(DateTime date)
{
if (date.Hour >= 22) return TimeSpan.Zero;
if (date.Hour < 8) date = date.Date.AddHours(8);
return date.Date.AddHours(22) - date;
}
// For the last day in our calculation
// End at 22:00 if the given date has an hour component that is later
static TimeSpan GetFrom0800(DateTime date)
{
if (date.Hour < 8) return TimeSpan.Zero;
if (date.Hour >= 22) date = date.Date.AddHours(22);
return date - date.Date.AddHours(8);
}
这里是合并开始日期和结束日期以及中间日期的代码流
// StartDate and EndDate variables as in your question.
TimeSpan result = GetTill2200(StartDate);
DateTime currentDate = StartDate.AddDays(1);
while (currentDate.Date < EndDate.Date)
{
// Add 14 hours
// Total: 17:54:52:3813
result = result.Add(TimeSpan.FromHours(14));
currentDate = currentDate.AddDays(1);
}
// returns 06:10:00.6187
// Total = 1.00:04:53 ( 1 days, 4 minutes, 53 seconds )
result = result.Add(GetFrom0800(EndDate));
// Prints 1.00:04:53
Console.WriteLine(result);