Date conversion

I get a datetime as 1/2/2010 11:29:30, which I show in a gridview. and I want to convert it to "February 1, 2010 at 11:29 a.m."

Please let me know how to convert it.

Thanks in advance.

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

+4
source share
3 answers

Use the ToString DateTime method, it is extremely useful in shaping 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 help with other formatting options, see here .

+2
source

This format should do this:

 "MMM d, yyyy a\\t hh:mm" 

just call the ToString DateTime instance and pass this string in the format

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

Source: https://habr.com/ru/post/1309258/


All Articles