How to convert the date string to date?

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

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)

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,825 questions
No comments
{count} votes

Accepted answer
  1. answered 2021-04-21T02:28:06.323+00:00
    Steven Young 256 Reputation points

    Dim dt As DateTime= DateTime.ParseExact("4/13/2021", "M/d/yyyy", Globalization.CultureInfo.InvariantCulture)

    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2021-04-20T07:23:58.47+00:00
    Viorel 82,371 Reputation points

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

    Dim culture As New System.Globalization.CultureInfo("en-ID") ' or "id-ID"

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

    No comments