我有一个网格视图和一个SqlDataSource
。
在 SqlDataSource
的更新语句中,我尝试将列的字符串转换为日期时间:
Date = Convert(DateTime, @Date, 104)
//this converts the string to mm.dd.yyyy .
我找不到mm.dd.yyyy hh:mm .
的代码
您的问题表明您正在使用 GridView 和数据源。
我假设您的源数据在SQL Server.
中采用日期时间格式如果是这样,请将其保留为该格式,否则,在返回时将其转换为 DateTime。
不要尝试在 SQL Server 端将其转换为字符串,也不要尝试对 DataTable 运行传递并使用 C# 将其转换为字符串。 这是错误的做法。
您需要做的就是在 GridView 的 BoundField 列上设置 DataFormatString,如下所示:
BoundField bf = new BoundField();
bf.DataField = "xxxxx";//The field from your DataSource you are binding to.
bf.HeaderText = "yyyyy";//The column header text of your GridView.
bf.DataFormatString = "{0:MM.dd.yyyy hh:mm}";
gv.Columns.Add(bf);//Add the column to your GridView.
要以所需的格式在网格中显示它,请使用 yourDate.ToString(yourFormat)
,例如:
DateTime.Today.ToString("mm.dd.yyyy")
CONVERT(varchar(17),Date,113)
使用这个,它会得到, 2011年11月29日 12:58
或者使用这个;
SELECT CONVERT(varchar(17),OrderDate,105)+' '+CONVERT(varchar(17),OrderDate,108) as Date FROM Orders
它会是 ; 2011-11-29 12:58:48
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "MM.dd.yyyy hh:mm",