Microsoft Word's date picker content control (which you can add to a document through the hidden-by-default Developer ribbon) uses date/time formatting strings that are slightly incompatible with DateTime.ToString. For example:
If I use the same format in DateTime.ToString:
DateTime.Parse("11/13/2014 12:00 PM").ToString("M/d/yyyy h:mm am/pm")
The result is "11/13/2014 12:00 a0/p0". The expected result is what Word displays, "11/13/2014 12:00 PM" (yes, PM is capitalized).
Is there a safe way to use a date format extracted from a Word content control to format a date in C#?
There is no
am
orpm
as a custom date and time format specifiers.You need to use
"tt"
custom format specifier which represents the entireAM
/PM
designator of yourCurrentCulture
.Your program thinks your
a
andp
characters as a literal string delimiter andm
specifier is for minutes. Since your single digit minute of yourDateTime
is0
, youram / pm
will bea0 / p0
And remember,
"/"
custom format specifier has a special meaning of replace me with current culture or supplied culture date separator. That means if yourCurrentCulture
'sDateSeparator
is not/
, your result will have your current date separator, not/
.