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 page is normal. When you use IIS after the release, the error shown in the figure above is confusing, 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)