When converting a string representation of a date and time to a DateTime object or any other temporal type with one of the available
system parsing methods, you should always provide an IFormatProvider parameter.
If you try to parse a string representation of a date or time without a format provider, the method will use the machine’s
CultureInfo; if the given string does not follow it, you’ll have an object that does not match the string representation or an unexpected
runtime error.
This rule raises an issue for the following date and time string representation parsing methods:
Parse ParseExact TryParse TryParseExact Of the following types:
System.DateOnly System.DateTime System.DateTimeOffset System.TimeOnly System.TimeSpan Alway use an overload of the parse method, where you can provide an IFormatProvider parameter.
Dim dateTimeString = "4/12/2023 4:05:48 PM" ' This is an en-US format string - 12 of April 2023
Dim dateTimeObject = DateTime.Parse(dateTimeString) ' This is wrongly parsed as 4th of December, when it's read in a machine with "CultureInfo.CurrentCulture" en-150 (English Europe)
Dim dateTimeString2 = "4/13/2023 4:05:48 PM" ' This is an en-US format string - 13 of April 2023
Dim dateTimeObject2 = DateTime.Parse(dateTimeString2) ' Runtime Error, when it's parsed in a machine with "CultureInfo.CurrentCulture" en-150 (English Europe).
Dim timeInSaudiArabia = New TimeOnly(16, 23).ToString(New CultureInfo("ar-SA"))
Dim timeObject = TimeOnly.Parse(timeInSaudiArabia) ' Runtime Error, when it's parsed in a machine with "CultureInfo.CurrentCulture" en-150 (English Europe).
Dim dateTimeString = "4/12/2023 4:05:48 PM" ' This is an en-US format string - 12 of April 2023
Dim dateTimeObject = DateTime.Parse(dateTimeString, New CultureInfo("en-US"))
Dim dateTimeString2 = "4/13/2023 4:05:48 PM" ' This is an en-US format string - 13 of April 2023
Dim dateTimeObject2 = DateTime.Parse(dateTimeString2, New CultureInfo("en-US"))
Dim timeInSaudiArabia = New TimeOnly(16, 23).ToString(New CultureInfo("ar-SA"))
Dim timeObject = TimeOnly.Parse(timeInSaudiArabia, New CultureInfo("ar-SA"))