locked
VB.Net convert String to Datetime RRS feed

  • Question

  • Hi there,

    In my VB.net project, a time String 8:40 a.m. was converted to date as shown in the code:
    Dim dtDate As Date
    dtDate = CDate(" 8:40 a.m.")

    it worked on Win7 32-bit without problem but it caused an Exception: "Conversion from string "8:40 a.m." to type 'Date' is not valid" in Win10 64-bit

    I tried replacing CDate() with Convert.ToDateTime() and run it in Win10 64-bit but got Got Exception: The string was not recognized as a valid DateTime. There is a unknown word starting at index 5
    The code is shown below:

    Dim strTime as String = “8:40 a.m.”
    Dim dtStart As DateTime = Convert.ToDateTime(strTime)
    Got Exception: The string was not recognized as a valid DateTime. There is a unknown word starting at index 5


    Could you please advise a solution?

    Thanks
    Jimmy-NZ

    Sunday, July 19, 2020 1:03 AM

All replies

  • This works for me on Windows 10 64-bit =>

    Dim dtfi = CType(Globalization.CultureInfo.InvariantCulture.DateTimeFormat.Clone(), Globalization.DateTimeFormatInfo)
    dtfi.AMDesignator = "a.m."
    dtfi.PMDesignator = "p.m."
    Dim dtDate As Date
    dtDate = DateTime.ParseExact("8:40 a.m.", "h:mm tt", dtfi)

    Sunday, July 19, 2020 6:33 AM
  • Hi Jimmy,
    try this:

    Module Module1
    
      Sub Main()
        Try
          Dim strTime As String = “8:40 a.m.”
          Dim d As Date = Date.Parse(strTime.Replace(".", ""), CultureInfo.InvariantCulture)
          Console.WriteLine(d)
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, July 19, 2020 6:56 AM
  • Maybe you should change the current language. For example:

    Dim strTime As String = “8:40 a.m.”

    Dim dtStart As DateTime = Convert.ToDateTime(strTime, CultureInfo.GetCultureInfo("mi-NZ"))

    or

    CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("mi-NZ")

    Dim strTime As String = “8:40 a.m.”

    Dim dtStart = CDate(strTime)

    There are several cultures that use “a.m.” and “p.m.”.


    • Edited by Viorel_MVP Sunday, July 19, 2020 7:51 AM
    Sunday, July 19, 2020 7:50 AM

  • There are several cultures that use “a.m.” and “p.m.”.


    Really, I know that beside in South Africa where a 24hours clock is used AM and PM are in the English language Culture, 

    But which one has a.m. and p.m. (with dots, must be a kind of coca cola culture)

    This code works fine by the way

     Dim x = "8:40 a.m.".Replace("."c, "")
     Dim y = CDate(x)

    To show it is not a problem from CDate

    Success
    Cor


    Sunday, July 19, 2020 9:33 AM
  • Hi Jimmy NZ2010,

    How is the question going? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 21, 2020 9:19 AM