asp.net提示信息_asp.net提示 该字符串未被识别为有效的 DateTime

时间:2018-08-20  来源:组件控件开发  阅读:


问题提出:在一个新闻信息添加网页的制作过程中,有一项要求记录新闻发布时间的字段。按要求,我先设计一个textbox控件,写入值:   this.timebox.Text = System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss");

在VS内置的开发服务器浏览时页面都是正常的,当发布后使用IIS时就出现了上图的错误,有点摸不着头脑,错误原因应该可以锁定为IIS导致的时间格式问题。

 


tempEntity.CreateTime = DateTime.ParseExact(mdr["CREATETIME"].ToString(), "yyyy-M-d H:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite);

 改为默认的即可:


tempEntity.CreateTime = Convert.ToDateTime(mdr["CREATETIME"].ToString());


这样当页面加载时,出现正常时间显示。

 

保存网页添加信息时:写入数据库值,应该把字符型数据重新转换为日期型:   

DateTime fbtime = Convert.ToDateTime(this.timebox.Text.Trim());

 
结题报错为:该字符串未被识别为有效的DateTime

 
解决办法:

 代码如下

输入值时改为:

this.timebox.Text = System.DateTime.Now.ToString("s");
 
取值是:

DateTime fbtime = DateTime.Parse(Convert.ToDateTime(this.timebox.Text.Trim()).ToString("yyyy-MM-dd"));


DateTime.Parse 方法 (String)


将日期和时间的指定字符串表示转换成其等效的 DateTime。

 代码如下

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, 1992, 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, 1992, 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, 1992, 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 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

DateTime.ParseExact Method (String, String, IFormatProvider)

asp.net提示信息_asp.net提示 该字符串未被识别为有效的 DateTime

http://m.bbyears.com/asp/43740.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新