Parsing Datetime with AM / PM ID
I need to parse Oct 3, 2014 10:05 a.m.
I am familiar with the usage DateTime.TryParseExact()
using an expression TT
for AM or PM. However, changing this parameter to T.T.
will not parse successfully string
to DateTime
.
Here is 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 something is missing or needs clarification.
source to share
This format is T.T.
not a standard format as you can see here , so my suggestion is for you to get rid of it with just:
"Oct 3, 2014 10:05 am" Replace ("am", "AM"). Replace ("pm", "PM");
Update
A more general solution, not too crowded by typing custom IFormatProvider
s, 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 don't have a C # compiler close by, I haven't hard-tested it). Which it finds for patterns a.m.
or p.m.
(ignoring case) and replaces with am
or pm
.
From now on, a will work TryParseExact
.
source to share