Formatting Time

GrexD

Well-known member
Joined
Mar 5, 2008
Messages
95
Programming Experience
Beginner
If it is 2:00 PM I want to return the hour as a single digit. I'm in VB.net in visual studio 2010. I can not get the hour to return a single digit '2'.


Error "Input string was not in the correct format"
Now.ToString("h")

Error "Input string was not in the correct format"
Now.ToString("H")

Retruns 02
'Now.ToString("hh")

Returns 14
Now.ToString("HH")

Anybody know how to do this?

Greg
 
Thanks for the link. I had seen that one, along with many others relating to formatting dates and time before posting. The trouble is, it doesn't really work when you are just trying to return the hour part in 12 hour format and store it in a variable. At least, I can't get it to work.

It says, "If the "H" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a Format Exception. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic."

If you go down to "Using Single Custom Format" it shows something like what's below. They use the single, lower case 'h' and it writes to the console window the single digit hour as you would expect. When I try to use this same format as I showed above to store it in a variable it throws an exception.

Dim dat1 As Date = Now

Console.WriteLine("'{0:%h}'", dat1)
Console.WriteLine("'{0: h}'", dat1)
Console.WriteLine("'{0:h }'", dat1)


Greg
 
What I ended up doing was....

Dim iHour As Integer = DatePart("H", Now)
If iHour > 12 Then
iHour -= 12
End If

Which works, but seems verbose.

Greg
 
When I try to use this same format as I showed above to store it in a variable it throws an exception.

For future reference, feel free to provide information about the exception, particularly the error message.

I just ran this code and it worked as expected:
VB.NET:
Dim dt As Date

dt = Date.Today.AddHours(1)

Console.WriteLine("{0:%h}", dt)

dt = Date.Today.AddHours(10)

Console.WriteLine("{0:%h}", dt)

dt = Date.Today.AddHours(13)

Console.WriteLine("{0:%h}", dt)

dt = Date.Today.AddHours(22)

Console.WriteLine("{0:%h}", dt)
 
Yes, that code does work. You essentially just wrote what I posted from the MS article. You're not doing anything different and so it really doesn't add to this thread.

Try this, write a line of code that will store the single digit value for the hour in a variable?
 
Give this a go:

VB.NET:
Dim h As Integer = Now.Hour.ToString()

The only problem is that it does show the hour in military time.

Hope this helps
-Josh
 
Josh,

Thanks, but that doesn't help at all. The "only problem" you point out is the whole point of this thread.

Greg
 
Try this, write a line of code that will store the single digit value for the hour in a variable?

VB.NET:
Dim iHour As Integer = Convert.ToInt32(String.Format("{0:%h}", Date.Now))

Mind you, I dont know how you are going to store any time past 9:59:59 in a single digit value :)
 
Thanks!

This is one of those little things in .Net that can be frustrating. It seems like it should be so simple and should be done like other formatting of dates with the .ToString() method.

I don't need to store it as an integer so, String.Format("{0:%h}", Date.Now) should do it.
 
Thanks, but that doesn't help at all. The "only problem" you point out is the whole point of this thread.

Sorry, didn't realize that :)
 
As suggested in article I linked to use formatting "%h".
VB.NET:
Dim hour As String = Date.Now.ToString("%h")
Note that String.Format("format", date) and date.ToString("format") performs same value formatting operation. String.Format is more extensible and has lots more formattion options, but if you just want to format a value use value.ToString method.
 
You may think you have, but you haven't actually provided a clear explanation of what you want. I think now that what you actually want is an Integer variable containing the hour in 12-hour time. You keep talking about "format" and those of us with experience know that formatting is only an issue when converting to a string, so we all assumed that you wanted a string, not a number.

As I said, formatting is only an issue when converting to a string, generally for display. There's no reason that the Framework should provide you with such a function. It's not something that would need to be done very often. Why bother when you can simply keep the data stored in a DateTime and then format as required when required.

Anyway, there's a flaw in the code you posted in post #4. For 12 AM you will get 0 and for 12 PM you will get 12. Surely it should either be 12 for both or 0 for both. You can stick with what you have, modified to fix that issue, or you can use ToString to create a String and then convert that back to a number using CInt or whatever.
 
Back
Top