The date type is used in. net today. The result runtime environment prompts "this string is not recognized as a valid DateTime". I will share the solution below to help you.
Question: During the creation of a webpage for adding news information, there is a field that requires recording the news release time. As required, I first design a textbox Control and write the value: this. timebox. text = System. dateTime. now. toString ("yyyy-mm-dd hh: mm: ss ");
When viewing pages on the built-in Development Server of VS, the pages are normal. When I use IIS after the release, I am confused, the error cause should be locked to the time format problem caused by IIS.
TempEntity. createTime = DateTime. parseExact (MCM ["CREATETIME"]. toString (), "yyyy-M-d H: mm: ss", CultureInfo. invariantCulture, DateTimeStyles. allowInnerWhite );
Change to the default one:
TempEntity. CreateTime = Convert. ToDateTime (MCM ["CREATETIME"]. ToString ());
In this way, when the page is loaded, the normal time is displayed.
When saving the webpage to add information: Write the Database Value and convert the complex data to the date type again:
DateTime fbtime = Convert. ToDateTime (this. timebox. Text. Trim ());
Error: the string is not recognized as a valid DateTime
Solution:
The Code is as follows: |
Copy code |
Change the input value: This. timebox. Text = System. DateTime. Now. ToString ("s "); Optional values: DateTime fbtime = DateTime. Parse (Convert. ToDateTime (this. timebox. Text. Trim (). ToString ("yyyy-MM-dd ")); |
DateTime. Parse method (String)
Converts the specified string of date and time to its equivalent DateTime.
The Code is as follows: |
Copy code |
Using System; Using System. Globalization; Namespace Parse { Class Class1 { Public static void Main (string [] args) { // Assume the current culture is en-US. // The date is Feburary 16,199 2, 12 hours, 15 minutes and 12 seconds. String myDateTimeValue = "2/16/1992 12:15:12 "; DateTime myDateTime = DateTime. Parse (myDateTimeValue ); Console. WriteLine ("1) myDateTime = {0}", myDateTime ); // Reverse month and day to conform to a different culture. // The date is Feburary 16,199 2, 12 hours, 15 minutes and 12 seconds. IFormatProvider culture = new CultureInfo ("fr-FR", true ); String myDateTimeFrenchValue = "16/02/1992 12:15:12 "; DateTime myDateTimeFrench = DateTime. Parse (myDateTimeFrenchValue, Culture, DateTimeStyles. NoCurrentDateDefault ); Console. WriteLine ("2) myDateTimeFrench = {0}", myDateTimeFrench ); // The date is Feburary 16,199 2, 12 hours, 15 minutes and 12 seconds. String [] expectedFormats = {"G", "g", "f", "F "}; MyDateTimeFrench = DateTime. ParseExact (myDateTimeFrenchValue, ExpectedFormats, Culture, DateTimeStyles. AllowWhiteSpaces ); Console. WriteLine ("3) myDateTimeFrench = {0}", myDateTimeFrench ); } } } /* This example yields the following results: 1) myDateTime = 2/16/1992 12:15:12 PM 2) myDateTimeFrench = 2/16/1992 12:15:12 3) myDateTimeFrench = 2/16/1992 12:15:12 */ |
DateTime. ParseExact Method (String, String, IFormatProvider)