Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my requirement is to compare date of birth is greater than date of joining in asp.net
Posted

Take a look at similar discussion: difference between two dates in year month and day[^]
 
Share this answer
 
Comments
Manas Bhardwaj 7-Jun-12 13:30pm    
5!
Maciej Los 7-Jun-12 13:33pm    
Thank you ;)
Use the DateTime.Compare method.

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

C#
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
 
Share this answer
 
Comments
Maciej Los 7-Jun-12 12:19pm    
Good answer, my 5!
Manas Bhardwaj 7-Jun-12 13:29pm    
Thanks
VJ Reddy 7-Jun-12 13:05pm    
Good answer. 5!
Manas Bhardwaj 7-Jun-12 13:29pm    
Thanks
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:
C#
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
 
Share this answer
 
v2
Comments
Maciej Los 7-Jun-12 12:19pm    
Good answer, my 5!
VJ Reddy 7-Jun-12 12:25pm    
Thank you, losmac :)
Manas Bhardwaj 7-Jun-12 13:29pm    
Well explained 5!
VJ Reddy 7-Jun-12 13:33pm    
Thank you, Manas :)
VB
public class DateCheck
{
public static void main(String[] args)
{
Date d1=new Date();
Date d2=new Date();
//passing date instances to an method which returns the max date
Date maxDate=Maxdate.max(d1,d2);
System.out.println("Maximum date is :"+maxDate);
}
}


DateTime.Compare Method [^]
 
Share this answer
 
Comments
Manas Bhardwaj 7-Jun-12 13:30pm    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900