如何在 C# 中比较 DateTime? [英] How to compare DateTime in C#?
本文介绍了如何在 C# 中比较 DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不希望用户提供回溯日期或时间.
I don't want user to give the back date or time.
如何比较输入的日期和时间是否小于当前时间?
How can I compare if the entered date and time is LESS then the current time?
如果当前日期和时间是 17-Jun-2010 , 12:25 PM ,我希望用户不能在 17 Jun -2010 之前提供日期和 12:25 PM 之前的时间.
If the current date and Time is 17-Jun-2010 , 12:25 PM , I want user cannot give date before 17 Jun -2010 and time before 12:25 PM.
如果用户输入的时间是 16-Jun-2010 和时间 12:24 PM,我的函数返回 false
Like my function return false if the time entered by user is 16-Jun-2010 and time 12:24 PM
推荐答案
MSDN:DateTime.Compare
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int 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);
// The example displays the following output:
// 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
这篇关于如何在 C# 中比较 DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文