检查日期范围是否在C#中是连续的? [英] Check if date range is sequential in c#?

查看:561
本文介绍了检查日期范围是否在C#中是连续的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个用户界面,用户可以在其中选择日期。有没有办法检查所选日期是否是连续的,例如:

Assume I have a user interface where the user can select days. Is there a way to check if the days selected are sequential, such as:

4 / 4、4 / 5、4 / 6、4 / 7、4 / 8 ,4 / 9、4 / 10或

4/4, 4/5, 4/6, 4/7, 4/8, 4/9, 4/10 or

4 / 29、4 / 30、5 / 1、5 / 2、5 / 3

4/29, 4/30, 5/1, 5/2, 5/3

我知道我可能可以遍历日期范围并进行检查,但是我更好奇是否已经有内置方法来检查这一点。

I know I probably can loop through the date range and check, but I was more curious if there was a built in method already to check for this.

关于上述情况,它们是有序的,可以过渡到下个月。

Regarding the above scenarios, they are in order and they can roll over into the next month.

我正在使用.NET Framework 2.0,并且不能使用LINQ。

I am using the .NET Framework 2.0 and can't use LINQ.

关于汤姆的答案:

DateTime dtStart = new DateTime(2011,5,4);
DateTime dtEnd = new DateTime(2011,5,11);

int numberOfDaysSelected = 7; //Assume 7 days were selected.

TimeSpan ts = dtEnd - dtStart;


if(ts.Days == numberOfDaysSelected - 1)
{
Console.WriteLine("Sequential");
}
else
{
Console.WriteLine("Non-Sequential");
}


推荐答案

我不相信一种内置的方法来实现所需的结果,但是如果您可以轻松分辨最早和最晚的日期,则可以通过从最新日期中减去最早的日期,然后验证该时间段的天数与所选日期的数量-1。

I do not believe there is a built in method to achieve your desired results but if you can easily tell the earliest and latest dates, you could create a new TimeSpan by subtracting the the earliest date from the latest date and then verifying that the number of days of the timespan matches the number of dates selected - 1.

这篇关于检查日期范围是否在C#中是连续的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆