检查两个日期之间的日期 [英] check a date between two dates
问题描述
大家好,
我有约会:-
2011年2月11日
25/12/2011
2011年9月12日
13/12/2011
而且我必须逐一检查这些日期,这些日期应介于2011年11月11日至2011年12月15日之间.
如果上述日期中的任何一个位于给定日期之间,则它应返回一个值以显示存在和不存在的日期,例如(1代表不存在,0代表不存在),并且循环不应移动更多.请帮我怎么做.
谢谢
Mohd wasif
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.
Thanks
Mohd wasif
推荐答案
List<DateTime> list = new List<DateTime> { Convert.ToDateTime("02/11/2011"), Convert.ToDateTime("25/12/2011"), Convert.ToDateTime("09/12/2011"), Convert.ToDateTime("13/12/2011") };
foreach(var date in list)
{
if ((date.CompareTo(Convert.ToDateTime("01/11/2011")) > 0) && (date.CompareTo(Convert.ToDateTime("15/12/2011")) < 1))
{
//return value
break;
}
}
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");
}
}
}
哦!..
Oh!..
bool InBetween(
ref System.DateTime toTest,
ref System.DateTime start,
ref System.DateTime end) {
return (start < toTest) && (toTest < end);
//or, depending on how you define "between":
return (start <= toTest) && (toTest <= end);
}
由于参数类型是值类型,因此出于性能考虑,数据是通过引用传递的,但实际上没有任何参数可以通过引用进行修改.如果删除"ref
",该代码将得到相同的结果.
换句话说,类型System.DateTime
使用常规比较运算符支持比较和顺序关系.这是执行比较的最易读和可维护的方式.请参阅:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ].
As parameter type is a value type, data is passed by reference for the sake of performance, but none of the parameters is actually modified through its reference. If you remove "ref
", the code will give the same results.
In other words, the type System.DateTime
supports comparison and order relationship using the customary comparison operators. This is the most readable and maintainable way of performing comparison. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].
这篇关于检查两个日期之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!