Asp.net prompts that the string is not recognized as a valid DateTime

Source: Internet
Author: User
Tags datetime iis trim valid


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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.