Use a specific cultural format to analyze the string as the date
Try to use the method as the date string of the "DD/MM/YYYY" format into a
object, which will cause abnormalities. It is prompted that the "string is not recognized as effective DateTime format".
Parse
Explanation and solution: DateTime
When parsing the string of the custom format, the current cultural format is set. In this example, the string represents the European date format, which is different from the default American format. To solve this problem, please use the method.
Method accept three parameters: the string to be parsed, the expected format and format provider. The format provider specifies the special format rules to be applied to the cultural format. For a given example:
Parse
The as a format provider will default to the setting of the current culture. Alternatively, you can specify a specific regional characteristics, such as: ParseExact
ParseExact
<code class="language-csharp">this.Text = "22/11/2009"; DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);</code>
null
Assume that the current culture format settings are assumed, and the limited date format set is supported.
<code class="language-csharp">DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", CultureInfo.GetCultureInfo("en-GB"));</code>
Need to be explicitly format, and allow more flexible customized analysis processes.
Select the best answer aspect:It provides greater flexibility by allowing custom target formats to make it more suitable for handling dates with non -standard formats or different cultures.
The above is the detailed content of How Can I Correctly Parse Culture-Specific Date Strings in C#?. For more information, please follow other related articles on the PHP Chinese website!