Saturday, March 31, 2012

System.FormatException: Input string was not in a correct format.

Is this a bug or am I going crazy??
WILL NOT WORK: DateTime.Now.ToString("h")
THIS ONE WILL THOUGH: DateTime.Now.ToString(" h")
But that space gets put in front of the hour.
Anything with "h" will work, as long as it's not by itself ??Shouldn't it be "hh" ?

"hh" returns the hour with a zero in front if less than 10. "h" is supposed to return the hour without the zero.
DateTime.Now.ToString(" h")
that works, but it won't work if the space is not in front of the h.
Have you tried using string.Format to see if it gives you the same results?
Here's a good reference for the format strings:
http://www.stevex.org/CS/blogs/dottext/articles/158.aspx
If I think of anything else, I'll let you know.

M$ doesn't seem to want you to know the current hour without the 0 in front of it.
I triedString.Format("{0:h}", DateTime.Now) and it has the same problem that theDateTime.Now.ToString("h") method has.
For some reason, you cannot use "h" all by itself. Luckily the way I'm using it allows me to leave it as " h" (with a space in front).
I'm using it in a JavaScript file like so:
var curH = <%= DateTime.Now.ToString(" h") %>;
So, the JavaScript removes the space for me.
My guess is it's a bug. Thanks for you help anyways, but I'm not going to worry about it.

northflacomps wrote:

Is this a bug or am I going crazy??
WILL NOT WORK: DateTime.Now.ToString("h")
THIS ONE WILL THOUGH: DateTime.Now.ToString(" h")
But that space gets put in front of the hour.
Anything with "h" will work, as long as it's not by itself ??


Actually, that behaviour appears to be "by design" and is documented in the MSDN help.
"DateTimeFormatInfo Class... Only format patterns listed in the second table above [which is the table with h and H and so on] can be used to create custom patterns; standard format characters listed in the first table [which is a table with the abbreviation codes such as T => Long Time Pattern] cannot be used to create custom patterns. Custom patterns are at least two characters long"
It appears that since the standard patterns are sometimes single-characters in length, they decided to force custom patterns to be at least 2 characters in length, to avoid collisions and ambiguity. And so on.
For padding, when the desired custom format pattern is only one character long, they apparently want one to use the "%" sign.
Here is some sample code.

PrivateSub TestFormatStringButton_Click( _
ByVal senderAs System.Object, _
ByVal eAs System.EventArgs _
)Handles TestFormatStringButton.Click

'RTE: "Input string was not the correct format".
'Me.Response.Write(DateTime.Now.ToString("h"))

'RTE: "Input string was not in a correct format".
'Me.Response.Write(DateTime.Now.ToString("H"))

'RTE: "Format specifier was invalid".
'Me.Response.Write(DateTime.Now.Hour.ToString("H"))

'This works.
Me.Response.Write(DateTime.Now.ToString("%h"))

EndSub


HTH.
--Mark Kamoski


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctFormat.asp
h
Displays the hour as a number without leading zeros using the 12-hour clock (for example,1:15:15 PM). Use%h if this is the only character in your user-defined numeric format.
Thanks for that information. I looked at MSDN for the DateTime structure but didn't see anything about the "%". Seems kinda dumb to me.

0 comments:

Post a Comment