Date Time conversion
I am getting datetime as 1/2/2010 11:29:30 which I am displaying in a gridview. and I want to convert it to "Feb 1, 2010 at 11:29".
Please let me know how开发者_Go百科 to convert it like this.
Thanks in advance.
Note: I am using asp.net with C#.
Use the ToString method of DateTime
, it is extremely useful in formating your dates. Here is a quick example:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Date
</HeaderTemplate>
<ItemTemplate>
<%# ((DateTime)Eval("DatePosted")).ToString("MMM d, yyyy 'at' h:mm tt")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For a reference on other formatting options, see here.
This format should do it:
"MMM d, yyyy a\\t hh:mm"
just call the ToString
of the DateTime instance and pass that string as a format
<asp headertext="CreationDate" :TemplateField>
<itemtemplate>
<asp id="Label1" runat="server"
Label.Text='<%# Bind("DatePosted", "{0:M-dd-yyyy at HH:MM}") %>'>;
</asp>
</itemtemplate>
</asp>
精彩评论