如何在C#中比较日期时间? [英] How to compare DateTime in C#?

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

问题描述

我不想用户,得到背面日期或时间。

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军-2010,下午12时25分,我想下午12时25分前,前6月17日-2010和时间,用户不能给出日期。

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.

就像我的函数返回false,如果用户输入的时间是16军-2010和时间下午12时24

Like my function return false if the time entered by user is 16-Jun-2010 and time 12:24 PM

推荐答案

MSDN:日期时间.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#中比较日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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