Can not Parse string to DateTime in Windows Phone 7

I am trying to convert the string to DateTime. But I can not convert.

DateTime dt = DateTime.Parse("16/11/2014", CultureInfo.InvariantCulture);
Console.WriteLine("Date==> " + dt);

The error is FormatException.

My input time format is "dd/MM/yyyy".

Please let me any idea to resolve my problem.

Jon Skeet
people
quotationmark

Given that you know your input format, you should specify it with `ParseExact:

DateTime dt = DateTime.ParseExact(text, "dd/MM/yyyy",
                                  CultureInfo.InvariantCulture);

I would always recommend being as explicit as you can be about date/time formats. It makes your intention very clear, and avoids the possibility of getting months and days the wrong way round.

As Soner has stated, CultureInfo.InvariantCulture uses MM/dd/yyyy as its short date pattern, as you can validate with:

Console.WriteLine(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)

As a mild plug, you might want to consider using my Noda Time project for your date/time handling - aside from anything else, that allows you to treat a date as a date, rather than as a date and time...

people

See more on this question at Stackoverflow