如何比较C#中的DateTime? [英] How to compare DateTime in C#?

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

问题描述

我不希望用户给予回覆的日期或时间。

I don't want user to give the back date or time.

如果输入的日期和时间是LESS,那么如何比较当前时间?

How can I compare if the entered date and time is LESS then the current time?

如果当前日期和时间是2010年6月17日,下午12:25,我希望用户不能在2010年6月17日之前的时间和12:25之前的时间。

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.

喜欢如果用户输入的时间是2010年6月16日和时间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: a href =http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx =noreferrer> DateTime.Compare

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

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