如何比较日期在c# [英] How to compare dates in c#

查看:121
本文介绍了如何比较日期在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期。一个日期是输入,而另一个是 DateTime.Now 。我有他们在 mm / dd / yyyy 格式,甚至可以是m / d / yy格式。这两个日期都为空,即数据类型为 DateTime?,因为我也可以传入null作为输入。现在我只想使用 mm / dd / yyyy m / d / yy 格式比较两个日期。 / p>

解决方案

如果您在DateTime变量中有日期,他们不会具有格式。 p>

您可以使用 日期 属性返回DateTime值,时间部分设置为午夜。所以,如果你有:

  DateTime dt1 = DateTime.Parse(07/12/2011 ); 
DateTime dt2 = DateTime.Now;

if(dt1.Date> dt2.Date)
{
//以后的日期
}
else
{
//这是一个较早或相同的日期
}


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屋!

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