【发布时间】:2014-03-03 11:52:36
【问题描述】:
如何在 c# 中更改日期格式,我正在从数据库中检索信息,在数据库中,日期格式存储在“dd/mm/yyyy”中,但是当我将这个日期输入我的表单时,它显示错误,该字符串不是有效的日期时间格式。例如“2/13/2014”,当我检索此日期时出现错误,我已尝试此代码
string orderd = row.Cells[1].Value.ToString();
DateTime dt = Convert.ToDateTime(lbl_date.Text);
DateTime orderdt = Convert.ToDateTime(orderd);
如何在检索时转换此日期。,
string orderd = row.Cells[1].Value.ToString();
DateTime orderdt = Convert.ToDateTime(orderd);
此代码也出现错误,row.cells[1].value 是我从 gridview 获取值,然后我正在转换为日期时间。但显示“字符串未被识别为有效的日期时间”。
【问题讨论】:
-
数据库中的列类型是什么?
Date?或varchar? -
试着把 Convert.ToDateTime( [value here]);
-
你的代码有
-,而样本有-。 -
“2/13/2014”是“mm/dd/yyyy”,而不是“dd/mm/yyyy”
-
请注意,如果数据库中有日期或日期时间字段,则根本不需要进行任何解析。