两个日期之间的比较 [英] Comparision between two dates

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

问题描述

我的要求是比较出生日期大于加入asp.net的日期.

my requirement is to compare date of birth is greater than date of joining in asp.net

推荐答案

看看类似的讨论: ^ ]
Take a look at similar discussion: difference between two dates in year month and day[^]


使用DateTime.Compare方法.

http://msdn.microsoft.com/en-us/library/system.datetime. compare.aspx [^ ]

Use the DateTime.Compare method.

http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx[^]

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


DateTime 结构中,关系运算符>, <, >=, <=等分别作为op_GreaterThan, op_LessThan, op_GreaterThanOrEqual, op_LessThanOrEqual重载,如下所述 ^ ].

因此,可以使用>运算符进行比较,该运算符在此处中解释. http://msdn.microsoft.com/zh-CN/library/system.datetime.op_greaterthan(v=vs.80) [
In the DateTime structure the relational operaters >, <, >=, <= etc. are overloaded as op_GreaterThan, op_LessThan, op_GreaterThanOrEqual, op_LessThanOrEqual respectively as explained here http://msdn.microsoft.com/en-US/library/system.datetime_methods(v=vs.80)[^].

Hence, the comparison can be made using the > operator explained here http://msdn.microsoft.com/en-US/library/system.datetime.op_greaterthan(v=vs.80)[^] using the DateTime objects as operands as shown below:
DateTime dateOfBirth = new DateTime(1992,6,7);
DateTime dateOfJoining = new DateTime(2012,8,25);

if(dateOfJoining > dateOfBirth)
	Console.WriteLine ("DOJ > DOB");
	
if(dateOfBirth > dateOfJoining)
	Console.WriteLine ("DOB > DOJ");
	
//Output
//DOJ > DOB


这篇关于两个日期之间的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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