Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 22-Jan-13 8:07am
Comments
DinoRondelly at 22-Jan-13 14:18pm
   
A simple google search would have solved this for you ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
DinoRondelly at 22-Jan-13 14:17pm
   
+5
Sergey Alexandrovich Kryukov at 22-Jan-13 14:25pm
   
Thank you, Dino.
—SA
Prathap Gangireddy at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The proper way to do this is actually this
var newValue = DateTime.ParseExact( "10/25/2000", "MM/dd/yyyy" );
  Permalink  
Comments
Prathap Gangireddy at 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.
Marcus Kramer at 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 at 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 at 22-Jan-13 14:28pm
   
I want the output to be a Datetime and not converting it into string.
Sergey Alexandrovich Kryukov at 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
Sergey Alexandrovich Kryukov at 22-Jan-13 14:31pm
   
Sure, my 5, but I don't think it makes sense do discuss anything with OP anymore. What else should be told?
—SA

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

  Print Answers RSS
0 OriginalGriff 534
1 Gihan Liyanage 298
2 ChintanShukla 295
3 RyanDev 250
4 Richard Deeming 250
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100