How to convert the date string to date?

Steven Young 261 Reputation points
2021-04-20T05:52:57.163+00:00

I used the code below to convert the date string to date, works well in most cases, but when the Globalization.CultureInfo.CurrentCulture.Name is "en-ID", it will give an error saying that “string was not recognized valid datetime”,how to fix it to adapt to users in various countries? thank you.

    Dim a As String = "4/13/2021"

    'Dim culture As New System.Globalization.CultureInfo(Globalization.CultureInfo.CurrentCulture.Name)

    Dim culture As New System.Globalization.CultureInfo("en-ID")' An error occur using this line

    Dim d As Date = Convert.ToDateTime(a, culture)

    MessageBox.Show(d.ToShortDateString)
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Viorel 111.8K Reputation points
    2021-04-20T07:23:58.47+00:00

    The next test shows that the order is day-month-year:

    Dim culture As New System.Globalization.CultureInfo("en-ID") ' or "id-ID"
    Console.WriteLine(DateTime.Now.ToString(culture))
    

    Therefore, your program is correct, but “4/13/2021” is not a valid date for this culture, because 13 is an invalid month.

    0 comments No comments