如何将 gridview 中的日期仅显示为日期而不是日期时间?

发布于 2024-11-09 06:48:35 字数 169 浏览 0 评论 0原文

我使用 gridview 从 sql server 数据库中提取日期值,并且日期从

2009年12月12日2009年12月12日上午12:00:00

如何防止这种情况发生?

谢谢 !

I am pulling in date values from a sql server database using a gridview and and the date gets converted from

12/12/2009 to 12/12/2009 12:00:00 AM

How do I prevent that from happening ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

苏佲洛 2024-11-16 06:48:35

您可以使用带有掩码的 ToString() 方法:

ToString("MM/dd/yyyy");

更新:刚刚意识到在您的情况下在网格视图模板中执行此操作会更容易

<asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />

You can use the ToString() method with a mask:

ToString("MM/dd/yyyy");

UPDATE: Just realized it would be easier in your case to do this in the grid view template

<asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />
甜中书 2024-11-16 06:48:35

您可以像这样在绑定列中设置日期格式

<itemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Bind("YourDateField", "{0:M-dd-yyyy}") %>'>;
</asp>
</itemtemplate>

You can set the date format in the bound column like this

<itemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Bind("YourDateField", "{0:M-dd-yyyy}") %>'>;
</asp>
</itemtemplate>
谎言月老 2024-11-16 06:48:35

将 dataformatstring 值设置为“{0:d}”

例如:

<asp:BoundField HeaderText="Date" DataField="Date_Field" ReadOnly="True" DataFormatString="{0:d}">
</asp:BoundField>

set the dataformatstring value to "{0:d}"

Ex:

<asp:BoundField HeaderText="Date" DataField="Date_Field" ReadOnly="True" DataFormatString="{0:d}">
</asp:BoundField>
黑寡妇 2024-11-16 06:48:35

之内

asp:Label runat="server" Text='<%# Eval("DateAndTime") %>'

尝试添加“{0:M-dd-yyyy}”

asp:Label runat="server" Text='<%# Eval("DateAndTime", "{0:M-dd-yyyy}") %>'

Within

asp:Label runat="server" Text='<%# Eval("DateAndTime") %>'

Try adding "{0:M-dd-yyyy}"

asp:Label runat="server" Text='<%# Eval("DateAndTime", "{0:M-dd-yyyy}") %>'

濫情▎り 2024-11-16 06:48:35

如果您已经在 RowDataBound 中操作日期,您还可以在 DateTime 对象上使用 .ToShortDateString()

You can also use .ToShortDateString() on the DateTime Object if you are already manipulating the date in the RowDataBound

晒暮凉 2024-11-16 06:48:35

您可以使用 DataAnnotations 属性和 DynamicField 控件;这样您就不必每次想要设置该字段的格式时都执行相同的格式设置。
这里有一个示例展示了如何执行此操作:
http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-8

You can use a DataAnnotations attribute and a DynamicField control; then you don't have to do the same formatting every time you want to format that field.
There is an example showing how to do this here:
http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-8

篱下浅笙歌 2024-11-16 06:48:35

当您从数据库中选择字段时,可以将其转换为选择中的字符串:

convert(varchar, myDate, 101)

when you select the field from the database you can convert it to a string in the select as:

convert(varchar, myDate, 101)
情绪少女 2024-11-16 06:48:35

尝试下面的代码:

    <asp:BoundField DataField="my_date" HeaderText="Date" 
                            ReadOnly="True" SortExpression="my_date" 
                            DataFormatString="{0:d}" />

在上面提到的代码中,my_date 是sqlserver 表的日期列。 DataFormatString="{0:d}" 是此代码的主要部分,用于解决您的特定问题。

Try the below code:

    <asp:BoundField DataField="my_date" HeaderText="Date" 
                            ReadOnly="True" SortExpression="my_date" 
                            DataFormatString="{0:d}" />

In the above mentioned code, my_date is the date column of the sqlserver table. The DataFormatString="{0:d}" is the main portion of this code to resolve the particular issue of yours.

想挽留 2024-11-16 06:48:35
for (int j = 0; j < gv_bill_dmd_process_create.Rows.Count; j++)
                {
                    GridViewRow row_fees = (GridViewRow)gv_bill_dmd_process_create.Rows[j];
                    TextBox gv_chk_bill_dept = row_fees.FindControl("txt_gv_DmdProsDuedate") as TextBox;
                    AjaxControlToolkit.CalendarExtender gv_chk_bill_dept1 = row_fees.FindControl("txt_gv_DmdProsDuedate_CalendarExtender") as AjaxControlToolkit.CalendarExtender;
                    gv_chk_bill_dept1.StartDate = fromdate;
                    gv_chk_bill_dept1.EndDate = todate;
                }
for (int j = 0; j < gv_bill_dmd_process_create.Rows.Count; j++)
                {
                    GridViewRow row_fees = (GridViewRow)gv_bill_dmd_process_create.Rows[j];
                    TextBox gv_chk_bill_dept = row_fees.FindControl("txt_gv_DmdProsDuedate") as TextBox;
                    AjaxControlToolkit.CalendarExtender gv_chk_bill_dept1 = row_fees.FindControl("txt_gv_DmdProsDuedate_CalendarExtender") as AjaxControlToolkit.CalendarExtender;
                    gv_chk_bill_dept1.StartDate = fromdate;
                    gv_chk_bill_dept1.EndDate = todate;
                }
丶视觉 2024-11-16 06:48:35

在获取日期字段的查询中使用它,

CONVERT(VARCHAR,date column name,103) as date

例如:select column1,column2,CONVERT(VARCHAR,date column name,103) as date from tablename

use this in query where you are getting date field

CONVERT(VARCHAR,date column name,103) as date

ex:select column1,column2,CONVERT(VARCHAR,date column name,103) as date from tablename

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文