C# 计算重叠日期
我收集了一系列活动日期,包括以下内容:C# 计算重叠日期,c#,datetime,C#,Datetime,我收集了一系列活动日期,包括以下内容: class EventDate { DateTime startDate; // the date when the event starts int lengthInHours; // how many hours the event will run for } 当添加新事件时,我需要检查以确保事件不会与集合中的任何现有事件重叠 进行此检查的优雅方法是什么?尝试以下方法: class EventDate { DateTi
class EventDate
{
DateTime startDate; // the date when the event starts
int lengthInHours; // how many hours the event will run for
}
当添加新事件时,我需要检查以确保事件不会与集合中的任何现有事件重叠
进行此检查的优雅方法是什么?尝试以下方法:
class EventDate
{
DateTime startDate; // the date when the event starts
int lengthInHours; // how many hours the event will run for
public bool Overlaps(EventDate otherDate)
{
double hours = (otherDate.startDate - startDate).TotalHours;
return (hours >= 0 && hours <= lengthInHours) ||
(hours < 0 && -hours <= otherDate.lengthInHours);
}
}
尝试以下方法:
class EventDate
{
DateTime startDate; // the date when the event starts
int lengthInHours; // how many hours the event will run for
public bool Overlaps(EventDate otherDate)
{
double hours = (otherDate.startDate - startDate).TotalHours;
return (hours >= 0 && hours <= lengthInHours) ||
(hours < 0 && -hours <= otherDate.lengthInHours);
}
}
尝试以下方法:
class EventDate
{
DateTime startDate; // the date when the event starts
int lengthInHours; // how many hours the event will run for
public bool Overlaps(EventDate otherDate)
{
double hours = (otherDate.startDate - startDate).TotalHours;
return (hours >= 0 && hours <= lengthInHours) ||
(hours < 0 && -hours <= otherDate.lengthInHours);
}
}
尝试以下方法:
class EventDate
{
DateTime startDate; // the date when the event starts
int lengthInHours; // how many hours the event will run for
public bool Overlaps(EventDate otherDate)
{
double hours = (otherDate.startDate - startDate).TotalHours;
return (hours >= 0 && hours <= lengthInHours) ||
(hours < 0 && -hours <= otherDate.lengthInHours);
}
}
您可以使用的TimePeriodChain类。集合确保链接时间段:
// ----------------------------------------------------------------------
public void TimePeriodChainSample()
{
TimePeriodChain timePeriods = new TimePeriodChain();
DateTime now = ClockProxy.Clock.Now;
DateTime testDay = new DateTime( 2010, 7, 23 );
// --- add ---
timePeriods.Add( new TimeBlock(
TimeTrim.Hour( testDay, 8 ), Duration.Hours( 2 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hours( 1, 30 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hour ) );
Console.WriteLine( "TimePeriodChain.Add(): " + timePeriods );
// > TimePeriodChain.Add(): Count = 3; 23.07.2010 08:00:00 - 12:30:00 | 0.04:30
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:30:00 | 01:00:00
// --- insert ---
timePeriods.Insert( 2, new TimeBlock( now, Duration.Minutes( 45 ) ) );
Console.WriteLine( "TimePeriodChain.Insert(): " + timePeriods );
// > TimePeriodChain.Insert(): Count = 4; 23.07.2010 08:00:00 - 13:15:00 | 0.05:15
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:15:00 | 00:45:00
// > Item: 23.07.2010 12:15:00 - 13:15:00 | 01:00:00
} // TimePeriodChainSample
您可以使用的TimePeriodChain类。集合确保链接时间段:
// ----------------------------------------------------------------------
public void TimePeriodChainSample()
{
TimePeriodChain timePeriods = new TimePeriodChain();
DateTime now = ClockProxy.Clock.Now;
DateTime testDay = new DateTime( 2010, 7, 23 );
// --- add ---
timePeriods.Add( new TimeBlock(
TimeTrim.Hour( testDay, 8 ), Duration.Hours( 2 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hours( 1, 30 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hour ) );
Console.WriteLine( "TimePeriodChain.Add(): " + timePeriods );
// > TimePeriodChain.Add(): Count = 3; 23.07.2010 08:00:00 - 12:30:00 | 0.04:30
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:30:00 | 01:00:00
// --- insert ---
timePeriods.Insert( 2, new TimeBlock( now, Duration.Minutes( 45 ) ) );
Console.WriteLine( "TimePeriodChain.Insert(): " + timePeriods );
// > TimePeriodChain.Insert(): Count = 4; 23.07.2010 08:00:00 - 13:15:00 | 0.05:15
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:15:00 | 00:45:00
// > Item: 23.07.2010 12:15:00 - 13:15:00 | 01:00:00
} // TimePeriodChainSample
您可以使用的TimePeriodChain类。集合确保链接时间段:
// ----------------------------------------------------------------------
public void TimePeriodChainSample()
{
TimePeriodChain timePeriods = new TimePeriodChain();
DateTime now = ClockProxy.Clock.Now;
DateTime testDay = new DateTime( 2010, 7, 23 );
// --- add ---
timePeriods.Add( new TimeBlock(
TimeTrim.Hour( testDay, 8 ), Duration.Hours( 2 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hours( 1, 30 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hour ) );
Console.WriteLine( "TimePeriodChain.Add(): " + timePeriods );
// > TimePeriodChain.Add(): Count = 3; 23.07.2010 08:00:00 - 12:30:00 | 0.04:30
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:30:00 | 01:00:00
// --- insert ---
timePeriods.Insert( 2, new TimeBlock( now, Duration.Minutes( 45 ) ) );
Console.WriteLine( "TimePeriodChain.Insert(): " + timePeriods );
// > TimePeriodChain.Insert(): Count = 4; 23.07.2010 08:00:00 - 13:15:00 | 0.05:15
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:15:00 | 00:45:00
// > Item: 23.07.2010 12:15:00 - 13:15:00 | 01:00:00
} // TimePeriodChainSample
您可以使用的TimePeriodChain类。集合确保链接时间段:
// ----------------------------------------------------------------------
public void TimePeriodChainSample()
{
TimePeriodChain timePeriods = new TimePeriodChain();
DateTime now = ClockProxy.Clock.Now;
DateTime testDay = new DateTime( 2010, 7, 23 );
// --- add ---
timePeriods.Add( new TimeBlock(
TimeTrim.Hour( testDay, 8 ), Duration.Hours( 2 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hours( 1, 30 ) ) );
timePeriods.Add( new TimeBlock( now, Duration.Hour ) );
Console.WriteLine( "TimePeriodChain.Add(): " + timePeriods );
// > TimePeriodChain.Add(): Count = 3; 23.07.2010 08:00:00 - 12:30:00 | 0.04:30
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:30:00 | 01:00:00
// --- insert ---
timePeriods.Insert( 2, new TimeBlock( now, Duration.Minutes( 45 ) ) );
Console.WriteLine( "TimePeriodChain.Insert(): " + timePeriods );
// > TimePeriodChain.Insert(): Count = 4; 23.07.2010 08:00:00 - 13:15:00 | 0.05:15
foreach ( ITimePeriod timePeriod in timePeriods )
{
Console.WriteLine( "Item: " + timePeriod );
}
// > Item: 23.07.2010 08:00:00 - 10:00:00 | 02:00:00
// > Item: 23.07.2010 10:00:00 - 11:30:00 | 01:30:00
// > Item: 23.07.2010 11:30:00 - 12:15:00 | 00:45:00
// > Item: 23.07.2010 12:15:00 - 13:15:00 | 01:00:00
} // TimePeriodChainSample
保存此类对象的集合应实现此验证。能否显示包含事件日期集合的代码?请参考此问题。保存此类对象的集合应实现此验证。能否显示包含事件日期集合的代码?请参考此问题。保存此类对象的集合应实现此验证。能否显示包含事件日期集合的代码?请参考此问题。保存此类对象的集合应实现此验证。能否显示包含事件日期集合的代码?请参考此问题。好极了。正是我要找的,甜心。正是我要找的,甜心。正是我要找的,甜心。这正是我想要的。