I am needing to parse Oct 3, 2014 10:05 a.m. I'm familiar with using the DateTime.TryParseExact() using the TT expression for the AM or PM. However modifying it to T.T. does not successfully parse the string into a DateTime.
Here's the code I have:
string dateContent = "Oct 3, 2014 10:05 a.m.";
DateTime parseDate;
bool attempt = DateTime.TryParseExact(content, "MMM d, yyyy hh:mm T.T.", CultureInfo.CurrentCulture, DateTimeStyles.None, out parseDate);
Any help would be greatly appreciated. Let me know if anything is missing, or needs clarification.
That T.T. format is not a standard format as you can see here, so my suggestion is for you to get rid of that with a simple:
"Oct 3, 2014 10:05 a.m.".Replace("a.m.", "AM").Replace("p.m.", "PM");
Update
A more general solution, not being too much overkill by entering into custom IFormatProviders, would be to use a regex like this:
Regex.Replace(dateContent, "([a|p]).(m).", "$1$2", RegexOptions.IgnoreCase);
This should do the trick (note that i do not have a c# compiler near by, tough i haven't test it).
What this does it to find for patterns a.m. or p.m. (ignoring the case) and replaces with am or pm.
From that point further a TryParseExact will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With