Click here to Skip to main content
15,892,927 members
Articles / Web Development / ASP.NET

how can i compare the dates in C#.net

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
15 Jun 2011CPOL 46.3K   1  
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...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
15 Jun 2011Sergey Alexandrovich Kryukov
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...
Please Sign up or sign in to vote.
15 Jun 2011Abdullatif M. Abu Al Rub
You should use "date time picker" control instead of text boxes this is more professional as a developer.
Please Sign up or sign in to vote.
15 Jun 2011NDebata
DateTime fromdate = Convert.ToDateTime(TextBox1.Text);DateTime todate = Convert.ToDateTime(TextBox2.Text);if (todate.CompareTo(fromdate) < 0){//add you logic}
Please Sign up or sign in to vote.
15 Jun 2011walterhevedeich
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.
Please Sign up or sign in to vote.
15 Jun 2011koolprasad2003
Convert the dates to DATETIME object withDateTime.ParseExact()and then compare with comparison operator <
Please Sign up or sign in to vote.
15 Jun 2011Ramalinga Koushik
Use this:DateTime fromdate = Convert.ToDateTime(TextBox1.Text);DateTime todate = Convert.ToDateTime(TextBox2.Text);if (todate.CompareTo(fromdate) < 0){//your code}
Please Sign up or sign in to vote.
17 Jun 2011UJimbo
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

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions