最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net提示 该字符串未被识别为有效的 DateTime
时间:2013-09-29 编辑:简简单单 来源:一聚教程网
问题提出:在一个新闻信息添加网页的制作过程中,有一项要求记录新闻发布时间的字段。按要求,我先设计一个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; namespace Parse string myDateTimeValue = "2/16/1992 12:15:12"; // Reverse month and day to conform to a different culture. IFormatProvider culture = new CultureInfo("fr-FR", true); // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string[] expectedFormats = {"G", "g", "f" ,"F"}; 1) myDateTime = 2/16/1992 12:15:12 PM |
DateTime.ParseExact Method (String, String, IFormatProvider)
-
上一个: C# WinForm程序退出示例与方法
-
下一个: C#统计字符串中某字符串出现次数示例
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10