检查两个日期之间的日期而不使用for循环 [英] Check a date between two dates without using for loop

查看:81
本文介绍了检查两个日期之间的日期而不使用for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有约会日期: -



02/11/2011

25/12/2011

09/12/2011

13/12/2011



我必须在2011年11月1日至2011年12月15日期间逐一检查这些日期。



如果上述任何一个日期介于两者之间给定的日期应该返回一个显示存在和不存在的日期的值,例如(1表示不存在,0表示不存在),循环不应该移动更多。请帮帮我怎么做。



我尝试过:



Hi All,

I have dates:-

02/11/2011
25/12/2011
09/12/2011
13/12/2011

and I have to check these dates one by one between 01/11/2011 and 15/12/2011.

If any one of above dates lies between given date than it should return a value showing date existing and non-existing e.g(1 for non-existing and 0 for non-existing ) and loop should not move more . please help me how to do it.

What I have tried:

protected void FindDate()
    {
        DateTime d1 = new DateTime(2011, 11, 2);
        
        DateTime StartDate = new DateTime(2011, 11, 01);
        DateTime EndDate = new DateTime(2011, 12, 15);
        int DayInterval = 1;

        List<datetime> dateList = new List<datetime>();
        while (StartDate.AddDays(DayInterval) <= EndDate)
        {
            StartDate = StartDate.AddDays(DayInterval);
            dateList.Add(StartDate);
            if (StartDate == d1)
            {
                Response.Write(d1.ToString() + " Exist");
            }
        }        
    }

推荐答案

为什么?

想想关于它:如果你的任何一个日期在开始日期和结束日期之间,那么它就在它们之间:在循环中将开始日期向上移动一天没有用。

相反,请考虑:

Why?
Think about it: if any one of your dates is between the start and end date then it's between them: moving the start date up a day in a loop does nothing useful.
Instead, consider this:
if (StartDate <= dt1 && dt1 <= EndDate)
   {
   Response.Write(string.Format("{0} exists.", dt1));
   }

如果你这样做是为了添加所有日期那么这很简单Linq:

If you are doing it to add all the dates then that's pretty simple Linq:

dateList.AddRange(Enumerable.Range(0, EndDate.Subtract(StartDate).Days + 1).Select(d => StartDate.AddDays(d)));


不确定你的意思是:



您可以使用多种方法检查日期范围中是否存在日期,例如,使用Linq:

获取计数日期范围内的日期

Not sure what you mean by saying:

You can check if date exists in a date range using several methods, for example, using Linq:
To get Count of dates in date range
List<DateTime> dates = new List<DateTime>();
dates.AddRange(new DateTime[]{new DateTime(2011, 11, 2),
	new DateTime(2011, 12, 25),
	new DateTime(2011, 12, 9),
	new DateTime(2011, 12, 13)});

DateTime startDate = new DateTime(2011, 11, 1);
DateTime endDate = new DateTime(2011, 12, 15);

var CountOfDatesInDateRange = dates.Count(x => x >= startDate && x <= endDate);

Console.WriteLine("Count of dates in date range is: {0}", CountOfDatesInDateRange);
//return 3





获取日期范围内的日期列表





To get list of dates in date range

var DatesRange = dates
	.Select(x=> new
		{
			Date = x,
			IsInRange = x>=startDate && x<= endDate
		});
		
foreach(var o in DatesRange)
{
	Console.WriteLine("{0} {1} date range: {2} - {3}", o.Date.ToString("yyyy/MM/dd"), o.IsInRange ? "is in" : "is NOT in", 
						startDate.ToString("yyyy/MM/dd"), endDate.ToString("yyyy/MM/dd"));
}
//returns:
//2011-11-02 is in date range: 2011-11-01 - 2011-12-15
//2011-12-25 is NOT in date range: 2011-11-01 - 2011-12-15
//2011-12-09 is in date range: 2011-11-01 - 2011-12-15
//2011-12-13 is in date range: 2011-11-01 - 2011-12-15


这篇关于检查两个日期之间的日期而不使用for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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