简体   繁体   中英

multiple date range and check if it is sequential. c# MVC

We have this project and one of the business requirement is it allows the client to input a multiple date range and check the individual dates if it is sequential/continuous or not to the others.

eq.

INPUT

startdate - enddate

10/24/2016 - 10/24/2016

10/26/2016 - 10/28/2016

OUTPUT

10/24/2016 - 10/24/2016 - NOT SEQUENTIAL

10/26/2016 - 10/26/2016 - SEQUENTIAL

10/27/2016 - 10/27/2016 - SEQUENTIAL

10/28/2016 - 10/28/2016 - SEQUENTIAL

For now I am playing around this solution Check if date range is sequential in c#? but i hope we i can find a better solution on how to properly do it.

Thank you and have a good day!

If by "sequential" we mean that the second date is the day after the first date then we can do the following:

private bool CheckSequential(DateTime date1, DateTime date2)
{
    // strips off time portion
    var d1 = date1.Date;
    var d2 = date2.Date;

    // add 1 to first date
    d1 = d1.AddDays(1);

    // compare them
    if(DateTime.Compare(d1, d2) == 0)
       return true;
    else
       return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM