列表中的AddDays(1)<;日期时间>;午夜到达时列出项目

本文关键字:午夜 gt 项目 时间 日期 AddDays 列表 lt | 更新日期: 2023-09-27 18:25:19

我有一个问题,有一个日期时间集合列表,以及从下拉列表中选择的日期,但时间段是从早上7点到凌晨4点(21小时)。我想修正这个集合中的日期,如果到达00或24小时(午夜12点),日期应该会更改。

(注意:所有项目的日期都是相同的,因为日期和时间是单独添加的)

这是我正在尝试的功能,但并不完美。

        private void CheckFixDateinList(List<TimeSlot> lstTimeSlots)
    {
        Boolean bHourChanged = false;
        int Fromlasthour = 0;
        int Tolasthour = 0;
        foreach(TimeSlot UEP in lstTimeSlots)
        {
            if (Fromlasthour < UEP.TimeSlotFrom.Hour)
                bHourChanged = true;
            if (bHourChanged)
                UEP.TimeSlotFrom = UEP.TimeSlotFrom.AddDays(1);
            Fromlasthour = UEP.TimeSlotFrom.Hour;

            if (UEP.TimeSlotFrom.Hour > Tolasthour)
                bHourChanged = true;
            if (bHourChanged)
                UEP.TimeSlotFrom = UEP.TimeSlotFrom.AddDays(1);
            if (bHourChanged )
                if (UEP.TimeSlotTo.Hour < UEP.TimeSlotFrom.Hour)
                    UEP.TimeSlotTo = UEP.TimeSlotTo.AddDays(1);
        }
    }

数据结构在这里

public class TimeSlot
{
    public string TimeSlot { get; set; }
    public int NumOfEmpl { get; set; }
    public DateTime CreateDate { get; set; }
    public int Deleted { get; set; }
    public string PDRowID { get; set; }
    public int Sequence { get; set; }
    public DateTime TimeSlotFrom { get; set; }
    public DateTime TimeSlotTo { get; set; }
    public TimeSlot()
    {
        this.CreateDate = DateTime.Now.Date;
        this.NumOfEmpl = 0;
        this.TimeSlot = "";
        this.Deleted = 0;
        this.PDRowID = "";
        this.TimeSlotFrom = DateTime.Now.Date;
        this.TimeSlotTo = DateTime.Now.Date;
    }
}

我该怎么解决这个问题?

列表中的AddDays(1)<;日期时间>;午夜到达时列出项目

我暂时得到了结果,但并不满意,觉得可以更好。

    private void CheckFixDateinList(List<TimeSlot> lstTimeSlots)
    {
        Boolean bHourChanged = false;
        int Fromlasthour = 0;
        int Tolasthour = 0;
        foreach(TimeSlot UEP in lstTimeSlots)
        {
            if (UEP.TimeSlotFrom.Hour < Fromlasthour)
                bHourChanged = true;
            if (bHourChanged)
                UEP.TimeSlotFrom = UEP.TimeSlotFrom.AddDays(1);
            Fromlasthour = UEP.TimeSlotFrom.Hour;
            if (bHourChanged )
              UEP.TimeSlotTo = UEP.TimeSlotTo.AddDays(1);
            if (UEP.TimeSlotTo < UEP.TimeSlotFrom)
            {
                bHourChanged = true;
                UEP.TimeSlotTo = UEP.TimeSlotTo.AddDays(1);
            }
        }
    }

如果(hours<5||hours==12(am))加1天,你能不能不检查一下?

嗯,我不确定我是否理解你在做什么,但这里有一个建议。

  public class TimeSlot
  {
     // some fields temporarily removed 
     public DateTime TimeSlotFrom { get; set; }
     public DateTime TimeSlotTo { get; set; }
     public TimeSlot()
     {
        // some fields temporarily removed 
        this.TimeSlotFrom = DateTime.Now.Date;
        this.TimeSlotTo = DateTime.Now.Date;
     }

     /// <summary>
     /// Method to move the end date forward one day if the start and end dates are the same and 
     /// the end time is less than the start time, for example start hour = 7 and end hour = 4.
     /// </summary>
     public void AdjustEndDateIfNecessary()
     {
        if (TimeSlotFrom.Date == TimeSlotTo.Date &&
            TimeSlotFrom.Hour > TimeSlotTo.Hour)
        {
           TimeSlotTo = TimeSlotTo.AddDays(1);
        }
     }

     /// <summary>
     /// Method to move the whole time slot (both start and end time) by incrementing by one day.
     /// </summary>
     public void IncrementTimeSlotByOneDay()
     {
        TimeSlotFrom = TimeSlotFrom.AddDays(1);
        TimeSlotTo = TimeSlotTo.AddDays(1);
     }
  }

  /// <summary>
  /// Method to go through a List{} of TimeSlot objects and do two things:
  /// - "normalize" them by fixing the end date if it is wrong
  /// - push time slots into the future if they overlap with the previous time slot. (This has a 
  /// cascading effect, so the last time slot may end up several days in the future.)
  /// </summary>
  private void CheckFixDateInList(List<TimeSlot> timeSlots)
  {
     DateTime highestEndTimeSoFar = DateTime.MinValue;
     foreach (TimeSlot timeSlot in timeSlots)
     {
        timeSlot.AdjustEndDateIfNecessary();
        while (timeSlot.TimeSlotFrom < highestEndTimeSoFar)
        {
           timeSlot.IncrementTimeSlotByOneDay();
        }
        highestEndTimeSoFar = timeSlot.TimeSlotTo;
     }
  }

请注意,我试图通过将代码的两部分放在TimeSlot类中,使事情变得更加面向对象。

我必须承认我没有测试过,部分原因是我甚至不确定我是否理解你在做什么。