【问题标题】:Date Without Time Using C# and InsertParameters (ASP.NET)使用 C# 和 InsertParameters 的无时间日期 (ASP.NET)
【发布时间】:2016-10-18 23:08:45
【问题描述】:

我在 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();

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

【问题讨论】:

  • String.Format("{0:dd/MM/YYYY}",DateTime.Now) 应该可以工作。
  • Date birth datetime 列是否在访问中?
  • 是的,但我需要使用 C# 而不是 SQL-query
  • 说到 String.Format - 我应该像这样写下代码吗? var f = DateTime.Now; <br/> string dwt = f.ToShortDateString();<br/> String.Format("{0:dd/MM/yyyy}", f);

标签: c# asp.net date


【解决方案1】:

我猜您的数据源将“出生日期”字段设置为 DateTime 类型,并且当您将字符串分配给它时,正在发生强制转换。

您是否尝试过像这样在 GridView 中设置字段格式:

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

【讨论】:

  • 是的,但我需要一个替代路径来格式化日期,使用 C#
  • 您需要将数据源中的字段设置为字符串类型,而不是 DateTime。否则,您正在编写这样的代码 DateTime t = (DateTime)(DateTime.Now.ToString("dd/mm/yyyy"))。
【解决方案2】:

试试ToShortDateString()方法

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

这只会导致date17/06/2016

【讨论】:

  • 发帖人已经在剪掉字符串的时间部分,并说它仍然不起作用。
猜你喜欢
  • 1970-01-01
  • 2017-01-18
  • 2012-10-07
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 2014-03-24
  • 2012-11-07
相关资源
最近更新 更多