3

I want to compare a date stored in a table in the form DD/MM/YYYY with the current date.

I need to know if it is earlier or later than DateTime.Now ...

Does someone have an idea to suggest?

Thank you in advance.

4

3 回答 3

5

您可以DateTime.Compare为此使用:

var result = DateTime.Compare(Convert.ToDateTime(TextBox1.Text), DateTime.Today);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);

有关详细信息,请参阅 MSDN 上的文档

于 2012-04-23T15:48:51.593 回答
1

您可以使用以下代码将时间戳解析为 DateTime 对象,然后根据需要进行比较。

DateTime date;
DateTime.TryParseExact("12/03/2009", "dd/MM/yyyy", null, DateTimeStyles.None, out date);

在此处查看更多信息http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

于 2012-04-23T15:54:44.283 回答
0

这可以通过DateTime.Compare方法来完成,如下所示:

date1 = Convert.ToDateTime(TextBox1.Text)
date2 = DateTime.Today
var result = DateTime.Compare(date1, date2)
string relationship

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
于 2012-04-23T15:54:28.340 回答