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

I want to compare the dates in asp.net using C#

my code is like this..

C#
DateTime FromDate = Convert.ToDateTime(txtFromDate.Text);
DateTime ToDate = Convert.ToDateTime(txtToDate.Text);

 if (FromDate <= ToDate )
  {
    ------
  }


error: String was not recognized as a valid DateTime.

by using 'MM/dd/yyyy' format we can get the result. But, my date format is in 'dd/MM/yyyy'.
can any one help me for this..

thanks in advance
Posted
Updated 7-Sep-12 2:52am
v3
Comments
ZurdoDev 7-Sep-12 8:52am    
If you set the CurrentCulture properly then it will work. See http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx
Timberbird 7-Sep-12 8:58am    
I believe using DateTime.ParseExact() method you can define any date time format, including custom. Setting format provider for DateTime.Parse() can help with standard formats

Use DateTime.Compare()[^]
Here is a sample code.

C#
string strFromDate = "26/03/2012";
string strToDate = "26/04/2012";

DateTime FromDate = DateTime.ParseExact(strFromDate, "dd/MM/yyyy",null);
DateTime ToDate = DateTime.ParseExact(strToDate, "dd/MM/yyyy", null);

int result = DateTime.Compare(FromDate, ToDate);

if (result < 0)
{
    //from date is less than to date

}
 
Share this answer
 
Comments
Vinay Raj K 7-Sep-12 9:21am    
It works.. thank you so much __TR__
__TR__ 7-Sep-12 9:31am    
Welcome :)
Menon Santosh 7-Sep-12 9:23am    
+5 nice work
__TR__ 7-Sep-12 9:31am    
Thank you Santosh.
Look at this[^] link.
 
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