Parse String to DateTime on Windows Phone

I want to parse a string of this format "4/14/2013 ‎1:04 PM" to a DateTime object. But actually this doesn't work, because I'm getting an error of type 'System.FormatException'. How can I fix the problem and convert my DateTime string to a DateTime object?

Thanks.

Upvotes: 1

Views: 1681

Answers (2)

Igor Ralic
Igor Ralic

Reputation: 15006

This works too!

var inputdt = "4/14/2013 1:04 PM";
var dt = DateTime.Parse(inputdt, CultureInfo.InvariantCulture);

Upvotes: 6

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

You input string is in en-us format, so you should specify correct CultureInfo format provider into DateTime.Parse method:

var ci = new CultureInfo("en-us");

var inputString = "4/14/2013 1:04 PM";
var dt = DateTime.Parse(inputString, ci);

Upvotes: 4

Related Questions