如何在 DataTable 类型 DateTime WEB 列中仅设置日期

发布于 2024-11-13 06:05:51 字数 595 浏览 3 评论 0原文

在此处输入图像描述大家好,

我的问题很简单,但请记住我没有将其绑定到任何网格或任何 ASP.NET 控件 我都有自己的网格控件,并且我想将其保留为日期时间列以用于排序目的。

我正在创建列类型为日期时间的数据表。

DataTable data = new DataTable();
data.Columns.Add("Invoice Date", typeof(DateTime));
DataRow dr = data.NewRow();
dr[0] = DateTime.Now;
 
//Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

当值显示在 ASP.NET 页面上时 它显示如下:

"2/28/2011 12:00:00 AM"

我有 2 列,在 1 列中我想仅显示日期,在其他列中我想将日期显示为“2011 年 12 月”,如果我使用带有字符串类型的 DataColumn,则可以实现这些格式但在这种情况下排序无法正常工作。

enter image description hereHi All,

My Question is simple but please keep in mind I'm not binding it to any grid or any of ASP.NET control I have my own grid control and I want to keep it as a DateTime Column for sorting purpose.

I'm creating DataTable With Column Type DateTime.

DataTable data = new DataTable();
data.Columns.Add("Invoice Date", typeof(DateTime));
DataRow dr = data.NewRow();
dr[0] = DateTime.Now;
 
//Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

When the Value is shown on ASP.NET page
it is show something like this:

"2/28/2011 12:00:00 AM"

I have 2 columns like this, In 1 column I want to show just date and in other column i want to show date as "Dec 2011", these formats can be achieved if i use DataColumn with type string but in that case sorting is not working properly.

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

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

发布评论

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

评论(2

优雅的叶子 2024-11-20 06:05:51

首先只取日期部分

dr[0] = DateTime.Now.Date;

其次使用

绑定值时的DateTime.ToShortDateString

    <asp:Label ID="Label1" runat="server" Text='<%# 
((DateTime)Eval("ItemValue")).ToShortDateString() %>'></asp:Label>

方法

编辑

不确定,但您可以检查此答案可能会帮助您解决问题:是否可以格式化数据表的日期列?

First of all take only date part

dr[0] = DateTime.Now.Date;

Second make use of

DateTime.ToShortDateString

    <asp:Label ID="Label1" runat="server" Text='<%# 
((DateTime)Eval("ItemValue")).ToShortDateString() %>'></asp:Label>

method to when binding value

EDIT

Not Sure but you can check this answer may help you to resolve your issue : Is it possible to format a date column of a datatable?

坠似风落 2024-11-20 06:05:51

你似乎混淆了你的概念。数据库及其内部表的目的是存储数据——而不是格式化数据。这是一个具体的实现细节,它是如何存储的。就像您不关心数据表是使用 UTF-8 还是 UTF-16 来存储字符串一样,您不应该关心它存储日期的格式。在这种情况下,它几乎肯定存储为纳秒数自 CE 0001 年 1 月 1 日午夜 12:00 起也就是说,如果您实际挖掘数据库的二进制格式,您会发现这些纳秒。

现在,UI 层(即 ASP.NET 页面)的用途是显示数据。 您可以在此处做出任何格式设置决定。

UI 层是您用来查看数据库的任何软件。该软件正在将一些纳秒数(内部存储格式)转换为字符串“2/28/2011 12:00:00 AM”。这样做的原因是因为这是显示时间点的默认格式。

您正在编码的 UI 层(您可以控制的 UI 层)现在需要选择如何将这些纳秒转换为文本。如果使用默认值,则会显示“2/28/2011 12:00:00 AM”。但如果您不想要默认值,则需要实际指定。 Pranay Rana 的回答给出了如何做到这一点的想法。 这是 UI 层的全部要点

You seem to be mixing your concepts. The purpose of the database, and the table inside it, is to store data---not to format it. It is an implementation detail exactly how this gets stored. Just like you wouldn't care whether the datatable uses UTF-8 or UTF-16 for storing your strings, you shouldn't care what format it stores a date in. In this case, it is almost certainly stored as the number of nanoseconds since 12:00 midnight, January 1, 0001 C.E. That is, if you actually dug around in the binary format of the database, you would find those nanoseconds.

Now, the purpose of your UI layer, i.e. the ASP.NET page, is to display the data. This is where you make any formatting decisions.

One UI layer is whatever software you're using to view the database. That software is converting some number of nanoseconds, which is the internal storage format, into the string "2/28/2011 12:00:00 AM". The reason it is doing this is because that is the default format for displaying points in time.

The UI layer you are coding---the one you have control over---now needs to choose how it converts those nanoseconds into text. If it goes with the default, it will display "2/28/2011 12:00:00 AM". But if you don't want the default, you need to actually specify that. Pranay Rana's answer gives an idea of how to do that. This is the whole point of your UI layer.

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