Click here to Skip to main content
15,919,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I am displaying set of data in Grid View.
The datetime field is displayed as "5/30/2012 12:00:00 AM" but I want to format it to DD-MM-YYYY.
I am new to .Net. Kindly guide me in formating this.

Regards,
AP
Posted

Check THis[^]

you can try this also
C#
<asp:TemplateField HeaderText="Departure Date">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("DepartDate",
"{0:dd-mm-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
 
Share this answer
 
v3
If you're using a BoundField, simply do as follows:
ASP.NET
<asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" xmlns:asp="#unknown" />

Just make sure you set the HtmlEncode attribute to False or it won't work.

Have a look: How To Set a Date Format In GridView Using ASP.NET[^]
 
Share this answer
 
Comments
Maciej Los 20-Jun-12 7:59am    
Good answer, my 5!
Prasad_Kulkarni 20-Jun-12 8:00am    
Thank you Isomac! Where're you?? din't saw any activity in some days. Work load??
CodeDZStar 20-Jun-12 8:07am    
will it output as "DD-MM-YYYY" format ?
Prasad_Kulkarni 20-Jun-12 8:18am    
Of course!
That's what your question was about, know??
Hi Arunprasath ,

set DataFormatString Property of the column that is bound to date as below sample code for a grid:


XML
<asp:gridview id="gvParent" runat="server" xmlns:asp="#unknown">
<columns> 
<asp:boundfield datafield="Leave_From_Date" headertext="Date From">DataFormatString = "{0:dd-MM-yyyy}" />  
</asp:boundfield></columns>
</asp:gridview>


this will do the trick.

Happy Coding :)
 
Share this answer
 
use
C#
format(date,"dd-MM-yyyy");
 
Share this answer
 
v2
hi,

you can do like this,

C#
DateTime inputDate = default(DateTime);
DateTime.TryParse("5/30/2012 12:00:00 AM", out inputDate);
return inputDate.ToString("dd/MM/yyyy");


Instead of TryParse, you can also use Parse, TryParseExact. hope this will solve your purpose.

thanks
-Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900