如何只显示日期?

问题描述 投票:0回答:4

我在数据表中有一个名为CurrentDate的列作为数据类型字符串(12/2/1983)。

当我在屏幕上显示时,显示为12/2/1983 12:00:00 AM。

我不知道为什么我在这里得到时间戳?

有人可以帮帮我吗?

asp.net
4个回答
2
投票

如果要从数据源(如SQL数据源)绑定它,则可以在查询中使用CONVERT:

CONVERT(VARCHAR(10),nameOfColumn,101) AS Date

如果需要在C#中执行此操作,请尝试ToShortDateString()方法:

string date = myDate.ToShortDateString();

1
投票

在C#中如果将其转换为DateTime对象:

DateTime.ToShortDateString();

1
投票

您是否正在使用数据绑定到gridview,formview或其他可格式化数据源?如果是这样,你可以简单地使用“d”格式标志,如下所示:

<asp:BoundField DataField="MyDate" DataFormatString="{0:d}" HeaderText="My Date" /> 

0
投票

在设置标签的上下文中使用它的一个例子是:

MyLabel.Text = DateTime.Now.ToString("A FORMATTING STRING HERE);

我使用的两个非常有用的MSFT帖子是:"Standard DateTime formatting strings""Custom date time formatting strings"

© www.soinside.com 2019 - 2024. All rights reserved.