Click here to Skip to main content
15,891,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

If I have two textboxes and I give two dates.

I am checking the second textbox date should be greater than the first textbox date.

How can I compare the dates in C#.net?

for e.g.

TextBox1.Text='2011-06-16T05:07:57+0000'
TextBox2.Text='2011-06-16T06:07:57+0000'

Please help me.
Posted
Updated 15-Jun-11 21:12pm
v4
Comments
Dalek Dave 16-Jun-11 3:12am    
Edited for Grammar and Readability.

Don't compare strings with string representation of dates, compare time values in the form of structure System.DateTime.

First of all, do you really have to enter this data in edit box? Is some date-time or calendar picker feasible? — it would provide you with the value of the structure in first place.

For example, look at this article:
DateTimePicker Web Control[^].

If you still want to get time from text, the conversion shown by Debata is correct.

When you have two instances of DateTime structure you can use operators "==", "!=" and ">", "<", ">=", "<=" in the sense "later", "earlier", "not earlier", "not later", respectively. Also, you can use subtraction operator ("-") which returns the result in the form of System.TimeSpan. This is a comprehensive set of operators to cover all your comparison needs.

—SA
 
Share this answer
 
v3
Comments
Dalek Dave 16-Jun-11 3:14am    
You are completely correct of course.
Sergey Alexandrovich Kryukov 16-Jun-11 3:19am    
Thank you for your confirmation, Dalek.
--SA
walterhevedeich 16-Jun-11 3:42am    
What I gave was an solution specific to the requirement. What you gave was what he needs. Have a 5. :)
Sergey Alexandrovich Kryukov 16-Jun-11 3:56am    
Thank you very much.
As you understand, what OP thinks is needed is not always what is really needed. :-)
--SA
You should use "date time picker" control instead of text boxes this is more professional as a developer.
 
Share this answer
 
v2
Comments
Dalek Dave 16-Jun-11 3:14am    
Sage Advice, have a 5!
Abdullatif M. Abu Al Rub 18-Jun-11 3:50am    
thanks
walterhevedeich 16-Jun-11 3:43am    
My 5 too.
Abdullatif M. Abu Al Rub 18-Jun-11 3:50am    
thanks too :)
Sergey Alexandrovich Kryukov 18-Jun-11 1:43am    
Now I see where your notion of "professional" goes... :-)
As to the DateTimePicker -- you're absolutely right, wise advice, my 5.
--SA
DateTime fromdate = Convert.ToDateTime(TextBox1.Text);
DateTime todate = Convert.ToDateTime(TextBox2.Text);
if (todate.CompareTo(fromdate) < 0)
{
//add you logic
}
 
Share this answer
 
Comments
CS2011 16-Jun-11 2:49am    
Correct Answer.Have 5+
RakeshMeena 16-Jun-11 2:52am    
5 from my side as well. You can as well use the static "Compare" method of the DateTime struct.
Sergey Alexandrovich Kryukov 16-Jun-11 2:59am    
My 4. Conversion is correct, this is very good, but 1) CompareTo is not as good as operators, also "-" operator should be mentioned, 2) the whole idea of conversion is not so good, because -- why using TextBox where the user can make mistakes; DateTimePicker is much better.

Please see my solution.
--SA
Dalek Dave 16-Jun-11 3:12am    
Well Done.
First, you need to convert the text to a DateTime value. From there, you can use DateTime.Compare[^] for this one. Check out the link.
 
Share this answer
 
Comments
CS2011 16-Jun-11 2:49am    
Correct Answer. 5+
walterhevedeich 16-Jun-11 2:57am    
Thanks.
Sergey Alexandrovich Kryukov 16-Jun-11 2:54am    
My 4. Correct, but: 1) there more operations and operators are better, 2) conversion needs some explanation about formats, 3) conversion from string is not a good design, DateTimePicker is much better.
Please see my answer.
--SA
Sergey Alexandrovich Kryukov 16-Jun-11 2:57am    
Conversion shown by NDebata is correct.
--SA
NDebata 16-Jun-11 3:11am    
Thank you sir
Convert the dates to DATETIME object with
DateTime.ParseExact()

and then compare with comparison operator <
 
Share this answer
 
Comments
Dalek Dave 16-Jun-11 3:14am    
Certainly a good option.
Vivek Krishnamurthy 16-Jun-11 3:20am    
Usign datetime picker would be best. If you have to use text box for some reason ParseExact. However ParseExact throws FormatException. better you use DateTime.TryParseExact. read more at
http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx
Use this:

DateTime fromdate = Convert.ToDateTime(TextBox1.Text);
DateTime todate = Convert.ToDateTime(TextBox2.Text);
if (todate.CompareTo(fromdate) < 0)
{
//your code

}
 
Share this answer
 
v2
Comments
Dalek Dave 16-Jun-11 3:15am    
Simple to correct.
[no name] 16-Jun-11 3:32am    
Thanks Dalek.
string s = "2011-06-16T05:07:57+0000";
string t = "2011-07-16T06:07:57+0000";

DateTime theDateA = DateTime.Parse(s);
DateTime theDateB = DateTime.Parse(t);

if (theDateB > theDateA)
{
   // blah blah
}


Give it a try
 
Share this answer
 
Comments
Db issues 29-Aug-11 2:24am    
i have a problem, when i select a date time picker less than the condition, i found error(that i specified), than i receive continuous error again and again, i didnt specify any loop there, what is the cause of that

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