Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix date format in ASP .NET BoundField (DataFormatString)?

Tags:

c#

.net

asp.net

I have a dynamic BoundField (for a DetailsView) with the following code:

BoundField bf1 = new BoundField(); bf1.DataField = "CreateDate"; bf1.DataFormatString = "{0:dd/MM/yyyy}"; bf1.HtmlEncode = false; bf1.HeaderText = "Sample Header 2";  dv.Fields.Add(bf1); 

But somehow, it still shows the wrong format: 2013-04-29T18:15:20.270.

Any way I could fix this for it to show "29/04/2013" instead? Thanks for your help.

like image 314
iceheaven31 Avatar asked May 03 '13 11:05

iceheaven31


2 Answers

You could add dataformatstring="{0:M-dd-yyyy}" attribute to the bound field, like this:

<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dd-M-yyyy}" /> 

source: cant format datetime using dataformatstring

like image 193
BornToCode Avatar answered Sep 28 '22 00:09

BornToCode


Formatting depends on the server's culture setting. If you use en-US culture, you can use Short Date Pattern like {0:d}

For example, it formats 6/15/2009 1:45:30 to 6/15/2009

You can check more formats from BoundField.DataFormatString

like image 28
Soner Gönül Avatar answered Sep 28 '22 02:09

Soner Gönül