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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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)
Dim dt As DateTime= DateTime.ParseExact("4/13/2021", "M/d/yyyy", Globalization.CultureInfo.InvariantCulture)
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.