繁体   English   中英

使用C#和InsertParameters的无时间日期(ASP.NET)

[英]Date Without Time Using C# and InsertParameters (ASP.NET)

我在ASP.NET中使用这个C#代码通过“AccessDataSource”在GridView中显示没有时间的日期:

var f = DateTime.Now;
string dwt = f.ToString("dd/MM/yyyy");

AccessDataSource3.InsertParameters["Date birth"].DefaultValue = dwt;
AccessDataSource3.Insert();

但仍然,应用程序显示如下日期: dd/MM/yyyy hh:mm:ss

我也试过这个:

string f = DateTime.Now.ToString("dd.MM.yyyy");
string[] v = f.Split(new char[] { ' ' });

AccessDataSource3.InsertParameters["Date birth"].DefaultValue = ""+v[0];
AccessDataSource3.Insert();

也不起作用。 也许这些代码不起作用,因为我忘记在其中添加一些东西?

尝试ToShortDateString()方法

var f = DateTime.Now;
string dwt = f.ToShortDateString();

这只会导致date17/06/2016年6 date

我猜你的数据源将'Date birth'字段设置为DateTime类型,当你将字符串分配给它时,正在进行强制转换。

您是否尝试在GridView中设置字段的格式,如下所示:

<asp:boundfield datafield="Date Birth"
        DataFormatString="{0:dd/MM/YYYY}"
        headertext="D.O.B"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM