Results 1 to 3 of 3

Thread: String was not recognized as a valid DateTime error with tt

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    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.

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: String was not recognized as a valid DateTime error with tt

    Quote Originally Posted by lleemon View Post
    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"

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width