Wednesday, January 5, 2011

Formatting DateTime databound fields in ASP.NET in asp:Label

Here's how to format it in code-behind code:
/* Displays the date and time as "05 Jan 2011 16:25"in a Label control inside a FormView control */
DateTime datetime = DateTime.Now; Label lblDateOfEntry = (Label)FormView1.FindControl("LabelDateOfEntry") lblDateOfEntry.Text = String.Format("{0:dd MMM yyyy HH:mm}", datetime); 

And here's how to format it in Markup code

<asp:Label ID="LabelDateOfEntry" runat="server" Text='<%# Bind("DateOfEntry","{0:dd MMM yyyy HH:mm}") %>' >

No comments: