两个日期范围之间的小时数重叠

时间:2013-12-26 14:39:13

标签: c# datetime overlap

我有几个日期范围,我想知道它们之间有多少小时重叠:

示例:

Range 1: from 1/1/2013 10:00AM to 1/1/2013 1:00PM
Range 2: from 1/1/2013 12:00PM to 1/1/2013 5:00PM

结果应为: 1小时

1 个答案:

答案 0 :(得分:1)

您正在寻找TimeSpan

  DateTime dtBegin, dtBegin1,  dtBegin2, dtEnd, dtEnd1, dtEnd2;
  dtBegin = dtBegin1 < dtBegin2 ? dtBegin1 : dtBegin2;
  dtEnd = dtEnd1 > dtEnd2 ? dtEnd1 : dtEnd2;
  TimeSpan range = dtEnd - dtBegin;
  int hours = range.Hours