Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

How to get a single digit hour string.format?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank Rizzo - 16 Jan 2008 21:37 GMT
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?
Peter Duniho - 16 Jan 2008 22:08 GMT
> Hello,
>
[quoted text clipped - 7 lines]
>
> 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
christery@gmail.com - 17 Jan 2008 19:10 GMT
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
christery@gmail.com - 17 Jan 2008 19:12 GMT
> 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...)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.