String was not recognized as a valid DateTime error with tt
Anyone know why I get an error when my time is PM? The MSDN doc says tt works with AM/PM.
Error I get is: String was not recognized as a valid DateTime
Code:
sDeadlineDtTm = "9/3/2011 1:30PM"
Try
dtDeadlineDtTm = DateTime.ParseExact(sDeadlineDtTm, "M/d/yyyy H:mmtt", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
Catch ex As Exception
Dim csType As Type = Me.GetType()
Dim sOutput As String = ex.ToString
ClientScript.RegisterStartupScript(csType, "startup", "<script>alert('Response Deadline error.');</script>")
Exit Sub
End Try
Thanks in advance.
Re: String was not recognized as a valid DateTime error with tt
Quote:
Originally Posted by
lleemon
Anyone know why I get an error when my time is PM? The MSDN doc says tt works with AM/PM.
Error I get is: String was not recognized as a valid DateTime
Code:
sDeadlineDtTm = "9/3/2011 1:30PM"
Try
dtDeadlineDtTm = DateTime.ParseExact(sDeadlineDtTm, "M/d/yyyy H:mmtt", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
Catch ex As Exception
Dim csType As Type = Me.GetType()
Dim sOutput As String = ex.ToString
ClientScript.RegisterStartupScript(csType, "startup", "<script>alert('Response Deadline error.');</script>")
Exit Sub
End Try
Thanks in advance.
It MSDN, it says this:
In the .NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.
Try:
sDeadlineDtTm = "9/3/2011 13:30PM"
Re: String was not recognized as a valid DateTime error with tt
On another note, you might want to use the TryParseExact:
http://msdn.microsoft.com/en-us/libr...arseexact.aspx
Rather than wrapping up in your own Try/Catch block.
Gary