如何格式化时间戳以仅在网格视图中显示日期 [英] How to format a time-stamp to show the date only in a grid view

查看:135
本文介绍了如何格式化时间戳以仅在网格视图中显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个aspx页面中,我绑定了这样的标签:

 < asp:TemplateField HeaderText =加入日期> 
< ItemTemplate>
< asp:Label ID =Label6runat =serverText ='<%#Eval(date_of_joining)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =Paid Priviledge Date>
< ItemTemplate>
< asp:Label ID =Label8runat =server
Text ='<%#Eval(paid_priviledge_date)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>

在后面的代码中,我绑定了这样的网格视图:(给出最小代码)

  GridView1.DataSource = dt2; 
GridView1.DataBind();

但是gridview列会显示这样的日期:

  4/12/2011 12:00:00 AM 
4/4/2011 12:00:00 AM



请建议如何删除时间戳记部分并仅显示日期部分。



我知道如何通过使用ToString和SubString进行格式化。但我无法在gridview中执行此操作。

解决方案

创建 FormatDate 方法在你的代码隐藏,并从你的gridview调用。
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

http://www.csharp-examples.net/string-format-datetime/



部分代码将放在你的代码中。
$ b $ pre $私有对象FormatDate(DateTime输入)
{
return String。格式({0:MM / dd / yy},输入);
}

这一点会在您的标记中显示出来

 < asp:TemplateField HeaderText =加入日期> 
< ItemTemplate>
< asp:Label ID =Label6runat =serverText ='<%#FormatDate(Eval(date_of_joining))%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =Paid Priviledge Date>
< ItemTemplate>
< asp:Label ID =Label8runat =server
Text ='<%#FormatDate(Eval(paid_priviledge_date))%>'>< / asp:标签>
< / ItemTemplate>
< / asp:TemplateField>

这就是我所说的 DRY 解决问题的方法。如果您需要以任何方式修改格式。您可以简单地编辑方法背后的代码,它会将甜蜜的爱带到您的所有标记。


In an aspx page I am binding the labels like this:

  <asp:TemplateField HeaderText="Date of Joining">
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# Eval("date_of_joining") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Paid Priviledge Date">
            <ItemTemplate>
                <asp:Label ID="Label8" runat="server" 
                    Text='<%# Eval("paid_priviledge_date") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

And in the code behind I'm binding the grid view like this :(minimum code is given)

GridView1.DataSource = dt2;
GridView1.DataBind();

But the gridview columns show the date like this :

4/12/2011 12:00:00 AM    
4/4/2011 12:00:00 AM

Please suggest how to remove the time stamp part and to display only the date part.

I know how to do this by formatting using ToString and SubString. But I am not able to do this in gridview.

解决方案

Create a FormatDate method in your codebehind, and call that from your gridview.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
http://www.csharp-examples.net/string-format-datetime/

This part will go in your code behind

private object FormatDate(DateTime input)
{
    return String.Format("{0:MM/dd/yy}", input);
}

And this bit will go in your markup

    <asp:TemplateField HeaderText="Date of Joining">
        <ItemTemplate>
            <asp:Label ID="Label6" runat="server" Text='<%# FormatDate(Eval("date_of_joining")) %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Paid Priviledge Date">
        <ItemTemplate>
            <asp:Label ID="Label8" runat="server" 
                Text='<%# FormatDate(Eval("paid_priviledge_date")) %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.

这篇关于如何格式化时间戳以仅在网格视图中显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆