How to Format Date in GridView in asp.net?

Introduction: In order to format DateTime  field in GridView’s ItemTemplate we have to pass format in Eval  . There are many format available for the DateTime  and  here i have given some example that demonstrate how to format DateTime and also all the other DateTime formats that can be used as per requirement are listed in the end.
  • If you are using ItemTemplate   to show Date of birth field in GridView then: .
<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="lblDOB" runat="server" Text='<%# Eval("Date", "{0:dd/MM/yyyy}")%>' ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

OR

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID=" lblDOB " runat="server" Text='<%# Eval("Date", "{0:D}")%>' ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
  • If you are using BoundField  to show Date of birth field in gridview then: .
<asp:BoundField DataField="DOB" DataFormatString="{0:d}" HeaderText="Date of birth" />

  • The following table shows some other date format strings that we can  be use to format date in GridView columns as per requirement.
Format Pattern
Name
Result Example
d
Short date
11/8/2008
D
Long date
Sunday, August 11, 2008
t
Short time
3:32 PM
T
Long time
3:32:00 PM
f
Full date/time (short time)
Sunday, August 11, 2008 3:32 PM
F
Full date/time (long time)
Sunday, August 11, 2008 3:32:00 PM
g
General date/time (short time)
8/11/2008 3:32 PM
G
General date/time (long time)
8/11/2008 3:32:00 PM
m or M
Month day
August 11
r or R
RFC 1123
Sun, 11 Aug 2008 8:32:00 GMT
s
Sortable date/time
2008-08-11T15:32:00
u
Universable sortable date/time
2008-08-11 15:32:00z
U
Universable sortable date/time
Sunday, August 11, 2008 11:32:00 PM
y or Y
Year month
August, 2008

Previous
Next Post »

1 comments:

Click here for comments
Anonymous
admin
August 29, 2013 ×

i should want read the date time in grideview ordered by date format date/month/year using vb.net

Congrats bro Anonymous you got PERTAMAX...! hehehehe...
Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..