467,161 Members | 1,023 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

How to get a single digit hour string.format?

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?
Jan 16 '08 #1
  • viewed: 2052
Share:
3 Replies
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
Jan 16 '08 #2
so combine the solutions, pick the 2 last (am/pm always 2 char) and
concat with htt... or the other way aroud.. not wanting pm5....
I like 23 telling me Is tiem to go to sleep...
//CY
Jan 17 '08 #3
On 17 Jan, 20:10, christ...@gmail.com wrote:
so combine the solutions, pick the 2 last (am/pm always 2 char) and
concat with htt... or the other way aroud.. not wanting pm5....
I like 23 telling me Is tiem to go to sleep...
//CY
Or parse int... (tiem=time...)
Jan 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mtech1@yadtel.net | last post: by
8 posts views Thread by Marc | last post: by
14 posts views Thread by thehobbit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.