On Wed, 16 Jan 2008 13:37:38 -0800, Frank Rizzo <no**@none.netwrote:
Hello,
I am trying to print out an hour in a format of 3pm or 5am. I've tried
the following, but it throws an exception saying invalid input string:
string times = string.Format("{0:h}{1:tt}", startTime, startTime);
However, if I change h to hh, it works fine, but then it prints out 03am
string times = string.Format("{0:hh}{1:tt}", startTime, startTime);
Does string.Format not support single digit hour?
It does. But not without a way to represent am/pm. Try:
string times = string.Format("{0:htt}", startTime);
or even more concise:
string times = startTime.ToString("htt");
The restriction seems arbitrary to me. Seems like a user ought to be able
to produce whatever string they want, even if it results in the loss of
information. But apparently the .NET designers felt differently. What
really bothers me is the inconsistency. The format "hh" by itself loses
the same information, but is allowed.
Anyway, there's a better way to do what you're doing anyway so the
inconsistent API shouldn't matter. :)
Pete