让我首先解释一下情况 - 假设我有4个日期:BS,BE,PS,PE(S表示开始,E表示结束)。 我需要知道在给定这些日期时有多少天过度研磨。 例如:BE-05.01,BS-10.01,PS-03.01,PE-07.01 结果是:3(05.01,06.01和07.01重叠)
我写了下面的代码,但它似乎很乱,我想检查是否有更简单的方法来做到这一点:
private static double GetNumOfOverLappingDays(DateTime BS, DateTime BE, DateTime PS, DateTime PE)
{
//case 1:
// |--- B ---|
// |----P ---|
//case 2:
// |--- B ---|
// | --- P --- |
//case 3:
// |--- B ---|
// | --- P ---- |
//case 4:
// |--- B ---|
// | - P - |
//case 5:
// |--- B ---|
// | -------- P -------- |
double days = -1;
bool isNotOverLap = (BS > PE) || (PS > BE);
if (isNotOverLap == false)
{
//case 1
if (BS == PS && BS == PE)
{
days = (PE - PS).TotalDays;
}
//case 2
else if (BE > PS && BE < PE)
{
days = (BE - PS).TotalDays;
}
//case 3
else if (BS > PS && BS < PE)
{
days = (PE - BS).TotalDays;
}
//case 4
else if (BS < PS && BE > PE)
{
days = (PE - PS).TotalDays;
}
//case 5
else if (BS > PS && BE < PE)
{
days = (BE - PS).TotalDays;
}
}
return days+1;
}
提前感谢您的任何帮助,
阿米特
答案 0 :(得分:9)
试试这个:
private static double GetOverlappingDays(DateTime firstStart, DateTime firstEnd, DateTime secondStart, DateTime secondEnd)
{
DateTime maxStart = firstStart > secondStart ? firstStart : secondStart;
DateTime minEnd = firstEnd < secondEnd ? firstEnd : secondEnd;
TimeSpan interval = minEnd - maxStart;
double returnValue = interval > TimeSpan.FromSeconds(0) ? interval.TotalDays : 0;
return returnValue;
}
答案 1 :(得分:1)
尝试使用for循环:
DateTime d1 = new DateTime(2013,12,1),
d2 = new DateTime(2013,12,14),
d3 = new DateTime(2013,12,10),
d4 = new DateTime(2013,12,20);
int count = 0;
for (var d = d1.Date; d <= d2.Date; d = d.AddDays(1))
{
if (d >= d3.Date && d <= d4.Date)
count++;
}
Console.WriteLine(count);
请注意,此代码提供整数值。如果您需要数小时的价值,此代码将不适合。这也不是最有效的方法,但它很简单,适用于小范围。
答案 2 :(得分:1)
如果你有两个强烈相关的值,比如开始和结束日期,那么最好有object which will hold them both。考虑创建DateRange
类:
public class DateRange
{
public DateRange(DateTime start, DateTime end)
{
if (start > end) throw new ArgumentException();
Start = start;
End = end;
}
public DateTime Start { get; private set; }
public DateTime End { get; private set; }
public double TotalDays { get { return (End - Start).TotalDays; } }
public bool Includes(DateTime value)
{
return (Start <= value) && (value <= End);
}
public DateRange Intersect(DateRange range)
{
if (range.Includes(Start))
return new DateRange(Start, (End < range.End) ? End : range.End);
if (range.Includes(End))
return new DateRange(range.Start, End);
if (Start < range.Start && range.End < End)
return range;
return null;
}
}
用法:
public static double GetNumOfOverLappingDays(DateRange b, DateRange p)
{
DateRange overlap = b.Intersect(p);
if (overlap == null)
return 0;
return overlap.TotalDays;
}
创建日期范围并将其传递给此方法:
var b = new DateRange(new DateTime(2013, 12, 20), new DateTime(2013, 12, 25));
var p = new DateRange(new DateTime(2013, 12, 22), new DateTime(2013, 12, 26));
double totalDays = GetNumOfOverLappingDays(b, p);
答案 3 :(得分:1)
我用它来获得两个时间范围之间的重叠范围,您可以从结果中轻松获得重叠天数
public static (DateTime, DateTime)? GetOverlappingTimeRange((DateTime, DateTime) firstTimeRange,
(DateTime, DateTime) secondTimeRange)
{
var leftIndex = Math.Max(firstTimeRange.Item1.Ticks, secondTimeRange.Item1.Ticks);
var rightIndex = Math.Min(firstTimeRange.Item2.Ticks, secondTimeRange.Item2.Ticks);
if (leftIndex > rightIndex)
{
return null;
}
else
{
return (new DateTime(leftIndex), new DateTime(rightIndex));
}
}
答案 4 :(得分:0)
我建议计算最新的开始和最早结束,然后减去两者。请注意,如果时间根本不重叠,此示例可以返回负数,但这不是您提到的5个案例之一。
static void Main(string[] args)
{
DateTime start1 = new DateTime(2013,1,2);
DateTime end1 = new DateTime(2013,1,14);
DateTime start2 = new DateTime(2013,1,1);
DateTime end2 = new DateTime(2013,1,12);
DateTime lateStart = GetLatest(start1, start2);
DateTime earlyEnd = GetEarliest(end1, end2);
Console.WriteLine((earlyEnd-lateStart).TotalDays + 1);
Console.ReadLine();
}
private static DateTime GetEarliest(DateTime start1, DateTime start2)
{
return start1 < start2 ? start1 : start2;
}
private static DateTime GetLatest(DateTime start1, DateTime start2)
{
return start1 > start2 ? start1 : start2;
}