1

Hi I have the following method and I'm passing it the value "07 Jan 2014 13:48:46" and from what I understand the TryParseExact should be matching the format "dd MMM yyyy hh:mm:ss" and returning true, however it is returning false, any ideas?

string[] formats= {"dd-MM-yyyy hh:mm:ss",
                   "dd MMM yyyy hh:mm:ss",
                   "dd MMM yyyy",
                   "hh-mm-ss",
                   "dd-MM-yyyy",
                   "dd-MM-yy",
                  };

 DateTime result;
 if (DateTime.TryParseExact(value, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
 {
     return result;
 }
 return null;
2
  • 3
    What is CurrentCulture? Commented Feb 25, 2014 at 15:53
  • CultureInfo.CurrentCulture is en-GB Commented Feb 25, 2014 at 15:55

2 Answers 2

15

24-hour time requires use of HH, not hh. Lowercase h is for 12 hour time.

See: http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

@NickSpicer Also remember that the : chars inside formats represent the time separator of the culture used. If your current culture uses a period, like bn-IN (Bangla (India)) does, it won't parse with a colon.
4

Because none of your format have 24-hour clock.

hh specifier is for 01 to 12. It doesn't have 13 as an hour.

Use HH specifier instead which is for 00 to 23.

For more information, take a look at;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.