I have a string “13/6/2011”.I have to convert it into DateTime. For this i am using
DateTime.ParseExact("13/6/2011","DD/MM/YYYY",null);
But it showing
String was not recognized as a valid DateTime
exception.
Any idea why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In addition to the “6” vs “06” problem that ChrisF has mentioned, your format string is incorrect. Day-of-month is “dd” and year is “yyyy”, so you should have a format of “dd/MM/yyyy”.
Additionally, I’d recommend explicitly passing in
CultureInfo.InvariantCulture, as otherwise the “/” could actually be treated as a different (culture-specific) date separator. (Another alternative is to quote the slashes, but I think using an invariant culture pins things down more.)In other words, I’d use something like this:
If you don’t necessarily have a two-digit day or month, use:
It’s not immediately clear to me whether that also copes with (say) 13/06/2011 though.