DateTime.Parse with custom date format

  • Thread starter Thread starter schouwla
  • Start date Start date
S

schouwla

How do I use DateTime.Parse or DateTime.ParseExact to create a
DateTime object from a string?

I tried this without luck:
string dateString = "27-02-2007";
// DateTime dt = DateTime.Parse(dateString);
// Throws a FormatException exception
IFormatProvider culture = new CultureInfo("fr-FR", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-mm-yyyy", culture);
Console.WriteLine(dt);

output shows : 1/27/2007 0:02:00

Which is not what I expect..

Lars
 
How do I use DateTime.Parse or DateTime.ParseExact to create a
DateTime object from a string?

I tried this without luck:
string dateString = "27-02-2007";
// DateTime dt = DateTime.Parse(dateString);
// Throws a FormatException exception
IFormatProvider culture = new CultureInfo("fr-FR", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-mm-yyyy", culture);
Console.WriteLine(dt);

output shows : 1/27/2007 0:02:00

Which is not what I expect..

Lars

Check your cases! m is the format string for minutes. What you want to use
is MM
 
Back
Top