gpt4 book ai didi

c# - 检查一周内的日期时间段是否可用

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:41 27 4
gpt4 key购买 nike

我有一个用户可以创建和添加规则的场景。该规则将被分配到一周中的某个时间段执行。假设添加了第二个规则,它不能在第一个时间段之间分配。

注意:我必须在一周内(即周一至周日)执行此操作。不存在第二周。这一单周逻辑将在全年的每一周运行。

通过我的尝试,如果一条规则从周二到周五运行,我只能为另一条规则分配周一或周六到周日的时间段,但无法预订/分配周六到周一的时间段(即, 一周的起点)。

以下是我到目前为止的尝试:

foreach (var item in rules)
{
DateTime StartDateWantToBook = Convert.ToDateTime((DateTime.Today.Month + "/" + StartDay + "/" + DateTime.Today.Year) + " " + StartDayTime);
DateTime EndDateWantToBook = Convert.ToDateTime((DateTime.Today.Month + "/" + EndDay + "/" + DateTime.Today.Year) + " " + EndDayTime);
DateTime StartDateAlreadyBooked = Convert.ToDateTime((DateTime.Today.Month + "/" + item.StartDay + "/" + DateTime.Today.Year) + " " + item.StartDayTime);
DateTime EndDateAlreadyBooked = Convert.ToDateTime((DateTime.Today.Month + "/" + item.EndDay + "/" + DateTime.Today.Year) + " " + item.EndDayTime);

//For same days and sun-sat days
if ((EndDateAlreadyBooked - StartDateAlreadyBooked).TotalSeconds >= 0)
{
if ((StartDateWantToBook <= StartDateAlreadyBooked && EndDateWantToBook <= StartDateAlreadyBooked) || (StartDateWantToBook >= EndDateAlreadyBooked && EndDateWantToBook >= EndDateAlreadyBooked)
|| (EndDateWantToBook <= StartDateAlreadyBooked && StartDateWantToBook >= EndDateAlreadyBooked))
{
IsExist = false;
}
else
IsExist = true;
}
else
{
if ((StartDateWantToBook <= StartDateAlreadyBooked && EndDateWantToBook <= StartDateAlreadyBooked) && (StartDateWantToBook >= EndDateAlreadyBooked && EndDateWantToBook >= EndDateAlreadyBooked))
{
IsExist = false;
}
else
IsExist = true;
}

if (IsExist == true)
{
break;
}
}

请有人建议应该编辑什么......或者新的逻辑来做到这一点......

EDIT : This is how my time slots are colored based on the rule..White spaces are unoccupied.

Rule Template View

最新编辑:

In my case i am taking Sunday=1, Monday=2.....Saturday=7. Let's have a scenario, where i booked Sunday 10:00 to Sunday 14:00. So my

ExistingStartDate : 7/1/2015 10:00 am

ExistingEndDate : 7/1/2015 02:00 pm

Now, i am booking another time slot from Monday 10:00 am to Sunday 06:00 pm So,

RequestedStartDate : 7/2/2015 10:00 am

RequestedEndDate : 7/1/2015 06:00 pm.

In this case no if() conditions passes..uploaded an image for reference..

最佳答案

根据您的描述,我的理解是槽中的任何部分重叠都足以阻止创建新记录。

假设这是您现有的时间段

         |-----------------|

以下请求会发生冲突

 (1)  |---------------|
(2) |------------------------ |
(3) |--------|
(4) |-----------|

您可以为所有这些场景编写条件,但我认为它可以归结为以下内容:

如果发生冲突

  • 请求的预订的开始或结束数据在现有预订的开始和结束日期内。这涵盖了场景 1、3 和 4
  • 现有预订的开始日期或结束日期在所请求预订的开始日期和结束日期之内。这涵盖了场景 2。

所以你的逻辑应该是……

if((StartDateWantToBook < EndDateAlreadyBooked) && (StartDateAlreadyBooked < EndDateWantToBook))

最后一点:您的变量名称非常相似 - 变量名称如 RequestedStartRequestedEndExistingStartExistingEnd 将使您的代码更具可读性和可维护性。

编辑:如果您不能阻止用户选择结束日期之后的开始日期,您可以执行以下操作之一...

// Throw an exception....
if (StartDateWantToBook > EndDateWantToBook)
throw new Exception("Start Date must occur before End Date");

或者...

// Correct the user's selection by swapping the values...
if (StartDateWantToBook > EndDateWantToBook)
{
DateTime temp = StartDateWantToBook ;
StartDateWantToBook = EndDateWantToBook;
EndDateWantToBook = temp;
}

throw new Exception("Start Date must occur before End Date");

关于c# - 检查一周内的日期时间段是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com