Pages

Monday, July 29, 2013

DateTime Format in GridView ItemTemplate In Asp.net C#

Introduction:


To format DateTime in GridView In ItemTemplate we have to pass format in eval or bind . There are many format available for the DateTime .In this article i have given some example that how to format datetime and at end i have listed the datetime format
used in gridview .

Following are  the examples of ItemTemplate where format used for datetime .

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="lblDate" runat="server" Text='<%# Eval("Date", "{0:dd/MM/yyyy}")%>' ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="lblDat1" runat="server" Text='<%# Eval("Date", "{0:D}")%>' ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>


Following are  the examples of BoundFieldwhere format used for datetime .
<asp:boundfield datafield="DOB" dataformatstring="{0:d}" headertext="Date Of Birth" readonly="True"> </asp:boundfield>

The following table shows some date format strings that can be used to format date in GridView columns,


Format PatternNameExample
dShort date11/8/2008
DLong dateSunday, August 11, 2008
tShort time3:32 PM
TLong time3:32:00 PM
fFull date/time (short time)Sunday, August 11, 2008 3:32 PM
FFull date/time (long time)Sunday, August 11, 2008 3:32:00 PM
gGeneral date/time (short time)8/11/2008 3:32 PM
GGeneral date/time (long time)8/11/2008 3:32:00 PM
m or MMonth dayAugust 11
r or RRFC 1123Sun, 11 Aug 2008 8:32:00 GMT
sSortable date/time2008-08-11T15:32:00
uUniversable sortable date/time2008-08-11 15:32:00z
UUniversable sortable date/timeSunday, August 11, 2008 11:32:00 PM
y or YYear monthAugust, 2008

No comments:

Post a Comment