Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DateTime.ToString() format question

2 views
Skip to first unread message

Jimmy [Used-Disks]

unread,
Sep 4, 2003, 5:03:54 PM9/4/03
to
It seems that I cannot not pass "h", "hh", "H", or "HH" (but nothing else)
to the ToString() method of a DateTime. It raises an excaption: "Input
string was not in a correct format."

So my question is: How can I convert the date time to a string, if I only
want the hours to show?

Thanks -

--
-Jimmy
Used-Disks:
http://www.used-disks.com/


Jay B. Harlow [MVP - Outlook]

unread,
Sep 4, 2003, 6:54:57 PM9/4/03
to
Jimmy,

> It seems that I cannot not pass "h", "hh", "H", or "HH" (but nothing else)
> to the ToString() method of a DateTime. It raises an excaption: "Input
> string was not in a correct format."

This is actually by design. For details read the Standard DateTime Format
Strings & Custom DateTime Format Strings.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondatetimeformatstrings.asp

A single character is interpreted as a Standard DataTime format, "H" & "h"
are not standard DateTime format strings. Hence those two cause exceptions.
A Custom DateTime format needs to be at least 2 characters.

Under VS.NET 2003 (.Net 1.1) "HH" & "hh" do not cause exceptions.

If you want just the hour, why not use the Hours property, then format that?

Dim d As DateTime
Dim s As String = d.Hour.ToString()

Of course you will get a 24 hour hour.

Hope this helps
Jay

"Jimmy [Used-Disks]" <noe...@used-disks.com> wrote in message
news:enGIOgyc...@TK2MSFTNGP11.phx.gbl...

Jimmy [Used-Disks]

unread,
Sep 5, 2003, 11:25:08 AM9/5/03
to
Thanks Jay!

--
-Jimmy Tharpe
http://www.aps-soft.com


0 new messages