如何比较C#日期 [英] How to compare dates in c#
问题描述
我有两个日期。一个日期输入等为 DateTime.Now
。我有他们在 MM / DD / YYYY
格式,它甚至可以是M / D / YY格式也。两个日期都是可空即,数据类型是的DateTime?
,因为我也可以传递null作为输入。现在,我想这两个日期仅与 MM / DD / YYYY比较
或 M / D / YY
格式。
如果您有您的日期,日期时间变量,他们不这样做的有无的格式。
您可以使用 日期
属性设置为午夜时间部分返回一个DateTime值。所以,如果您有:
<预类=郎-CS prettyprint-覆盖>
的DateTime DT1 =日期时间。解析(07/12/2011);
日期时间DT2 = DateTime.Now;
如果(dt1.Date> dt2.Date)
{
//这是一个日后
}
,否则
$ { b $ b //这是一个较早或等于日期
}
I have two dates. One date is input and other is DateTime.Now
. I have them in mm/dd/yyyy
format, it can even be m/d/yy format also. Both dates are nullable i.e, datatype is DateTime?
, since I can pass null also as input. Now I want to compare the two dates only with mm/dd/yyyy
or m/d/yy
format.
If you have your dates in DateTime variables, they don't have a format.
You can use the Date
property to return a DateTime value with the time portion set to midnight. So, if you have:
DateTime dt1 = DateTime.Parse("07/12/2011");
DateTime dt2 = DateTime.Now;
if(dt1.Date > dt2.Date)
{
//It's a later date
}
else
{
//It's an earlier or equal date
}
这篇关于如何比较C#日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!