Zerotoinfinity
Zerotoinfinity

Reputation: 6530

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 to convert it like this.

Thanks in advance.

Note: I am using asp.net with C#.

Upvotes: 4

Views: 622

Answers (3)

wsanville
wsanville

Reputation: 37516

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.

Upvotes: 2

Sparky
Sparky

Reputation: 15085

<asp headertext="CreationDate" :TemplateField>
   <itemtemplate>
    <asp id="Label1" runat="server"
 Label.Text='<%# Bind("DatePosted", "{0:M-dd-yyyy at HH:MM}") %>'>;
    </asp>
   </itemtemplate>
  </asp>

Upvotes: 0

Oskar Kjellin
Oskar Kjellin

Reputation: 21870

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

Upvotes: 1

Related Questions