Click here to Skip to main content
Sign Up to vote bad
good
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:07

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

2 solutions

  Permalink  
Comments
DinoRondelly - 22 Jan '13 - 14:17
+5
Sergey Alexandrovich Kryukov - 22 Jan '13 - 14:25
Thank you, Dino. —SA
Prathap Gangireddy - 22 Jan '13 - 14:23
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:25
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
var newValue = DateTime.ParseExact( "10/25/2000", "MM/dd/yyyy" );
  Permalink  
Comments
Prathap Gangireddy - 22 Jan '13 - 14:22
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 - 22 Jan '13 - 14:26
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:29
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:28
I want the output to be a Datetime and not converting it into string.
Sergey Alexandrovich Kryukov - 22 Jan '13 - 14:30
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 - 22 Jan '13 - 14:31
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,356
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


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