Click here to Skip to main content
16,009,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
How to compare two dates formats(MM/dd/yyyy) using c#.net?.
Ex:
C#
string dt="16/01/2014";
string dt0="12/31/2013";
if(dt != dt0)
{
} 

like this,how?
Pls help me. Thank u.
Posted
Updated 15-Jan-14 21:19pm
v3

Try like this..

C#
string dt = "16/01/2014";
       string dt0 = "12/31/2013";
       DateTime dt_date0 = DateTime.ParseExact(dt, "dd/MM/yyyy", null);
       DateTime dt_date1 = DateTime.ParseExact(dt0, "dd/MM/yyyy", null);
       if (dt_date0 != dt_date1)
       {

}

Reference : DateTime.ParseExact[^]
 
Share this answer
 
Comments
[no name] 16-Jan-14 3:59am    
Am getting date like 1/16/2014,how it is convert to 01-16-2014? pls help me.
Karthik_Mahalingam 16-Jan-14 4:05am    
try like this
// 1/16/2014,01-16-2014
string input = "1/16/2014";
var ary = input.Split('/');

var dt = new DateTime( Convert.ToInt32( ary[2]), Convert.ToInt32(ary[0]), Convert.ToInt32(ary[1])).ToString("MM-dd-yyyy");
[no name] 16-Jan-14 4:41am    
+5
Karthik_Mahalingam 16-Jan-14 4:43am    
thanks
[no name] 16-Jan-14 4:11am    
Thank u karthik,thank u so much.
C#
DateTime dt = new DateTime();
 dt = DateTime.ParseExact("your date here", "MM/dd/yyyy", null);  //converting string into standard datetime format



convert both dates using above code and then compare
 
Share this answer
 
v2
Try this..


DateTime datet ,date0;
             string dt = "16/01/2014"; //  dd/MM/yyyy.
             string dt0 = "12/31/2013"; // MM/dd/yyyy

            string[] splitdate=new string[3];
            splitdate = dt.Split('/');


            dt = splitdate[1].ToString() + "/" + splitdate[0].ToString() + "/"  +splitdate[2].ToString();
            datet = Convert.ToDateTime(dt);
            date0 = Convert.ToDateTime(dt0);
            if (datet != date0)
            { 
            }
 
Share this answer
 
v3

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