Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to compare the previous,current and future date? i wanna to compare this date,one is grater or less than each other.then i will do the process contain in ma codes
Posted
Updated 30-May-13 22:06pm
v2

You can use DateTime.Compare[^] Method for this.

The example on MSDN:
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";


=OR=
You can use TimeSpan for the same purpose. TimeSpan[^] Structure on MSDN

The example on MSDN:
C#
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;


Good luck,
OI
 
Share this answer
 
v2
You can use DateTime class CompareTo method to compare. Here is the details :
http://msdn.microsoft.com/tr-tr/library/5ata5aya.aspx[^]
 
Share this answer
 
var result = DateTime.Compare(Convert.ToDateTime(TextBox1.Text), DateTime.Today);
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);



Kishor Makwana
Software Engineer
Insight Softech
www.insightsoftech.com
 
Share this answer
 

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