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

Source: Internet
Author: User

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)

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.