DateTime.Parse with custom date format

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
 
R

Rad [Visual C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top