Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Is there a way to Convert MM/dd/yyyy string to datetime using C#.

I have tried various methods but the problem is when I convert the datetime format is

dd/MM/yyyy and not MM/dd/yyyy.

Can anyone please help.

Thanks.
Posted
Comments
DinoRondelly 22-Jan-13 14:18pm    
A simple google search would have solved this for you ...

 
Share this answer
 
Comments
DinoRondelly 22-Jan-13 14:17pm    
+5
Sergey Alexandrovich Kryukov 22-Jan-13 14:25pm    
Thank you, Dino.
—SA
Prathap Gangireddy 22-Jan-13 14:23pm    
Hi Dino and Sergey,

I have done enough google search..didn't get the solution.

I have tried the below code string fromdate = DTPFromDate.Value.ToString("MM/dd/yyyy"); string todate = DTPToDate.Value.ToString("MM/dd/yyyy"); IFormatProvider culture = new CultureInfo("en-GB"); DateTime dtfromdate = DateTime.ParseExact(fromdate, "MM/dd/yyyy",CultureInfo.GetCultureInfo("en-gb")); But the output is dd/MM/yyyy.
Sergey Alexandrovich Kryukov 22-Jan-13 14:25pm    
Read again, this time with some thinking...
At least look at code samples available on these pages, they show how to do it correctly.
—SA
The proper way to do this is actually this
C#
var newValue = DateTime.ParseExact( "10/25/2000", "MM/dd/yyyy" );
 
Share this answer
 
Comments
Prathap Gangireddy 22-Jan-13 14:22pm    
Hi All,

Thanks for the replies.

I have tried the below code

string fromdate = DTPFromDate.Value.ToString("MM/dd/yyyy");
string todate = DTPToDate.Value.ToString("MM/dd/yyyy");
IFormatProvider culture = new CultureInfo("en-GB");
DateTime dtfromdate = DateTime.ParseExact(fromdate, "MM/dd/yyyy",CultureInfo.GetCultureInfo("en-gb"));

But the output is dd/MM/yyyy.

Please suggest something new.
fjdiewornncalwe 22-Jan-13 14:26pm    
If you want your output to be different, just output it with dtfromDate.ToString("MM/dd/yyyy"). As long as it is parsing correctly, you can output your date any which way you would like by adding a format string to the ToString call.
Sergey Alexandrovich Kryukov 22-Jan-13 14:29pm    
Are you serious? Who told you that you should right this gibberish? Did any of the answers by Marcus or myself advice you to do something like that? Where did you find it?

Look, what's the use of giving your advice if you don't even try to follow it?
—SA
Prathap Gangireddy 22-Jan-13 14:28pm    
I want the output to be a Datetime and not converting it into string.
Sergey Alexandrovich Kryukov 22-Jan-13 14:30pm    
Who, who gave you an advice to convert anything to string?! where?
Are you just trolling? will be reported as such...
—SA

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