Date Range Overlap with Nullable Dates

前端 未结 4 1665
被撕碎了的回忆
被撕碎了的回忆 2021-02-10 03:23

I\'m looking for an extended answer to the question asked here:

Determine Whether Two Date Ranges Overlap

where any of the dates in either date range can be null

4条回答
  •  温柔的废话
    2021-02-10 04:04

    All answers are based if the condition is true. I'would like to add some note here.

    1- The DateTime variable type is a struct and you can not set it to null unless that you are using nullable type like "DateTime?"

    2- To find the overlap range follow the following steps

    DateTime? StartOverLap = null,EndOverLap = null;
                if (StartA != null && StartB != null)
                {
                    StartOverLap = StartA > StartB ? StartA : StartB;
                }
                else if (StartA == null && StartB != null)
                {
                    StartOverLap = StartB;
                }
                else if (StartA != null && StartB == null)
                {
                    StartOverLap = StartA;
                }
                if (EndA != null && EndB != null)
                {
                    EndOverLap = EndA < EndB ? EndA : EndB;
                }
                else if (EndA == null && EndB != null)
                {
                    EndOverLap = EndB;
                }
                else if (EndA != null && EndB == null)
                {
                    EndOverLap = EndA;
                }
                if (StartOverLap != null && EndOverLap == null)
                {
                    if (EndOverLap < StartOverLap)
                    {
                        StartOverLap = null;
                        EndOverLap = null;
                    }
                }
    

提交回复
热议问题