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.
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
very simple just add this to your bound field DataFormatString="{0: yyyy/MM/dd}"
The following links will help you:
In Client side design page you can try this: {0:G}
OR
You can convert that datetime format inside the query itself from the database:
I had the same problem, only need to show shortdate (without the time), moreover it was needed to have multi-language settings, so depends of the language, show dd-mm-yyyy or mm-dd-yyyy.
Finally using DataFormatString="{0:d}
, all works fine and show only the date with culture format.
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}" />
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
In The above link you will find the answer
**C or c**
Displays numeric values in currency format. You can specify the number of decimal places.
Example:
Format: {0:C}
123.456 -> $123.46
**D or d**
Displays integer values in decimal format. You can specify the number of digits. (Although the type is referred to as "decimal", the numbers are formatted as integers.)
Example:
Format: {0:D}
1234 -> 1234
Format: {0:D6}
1234 -> 001234
**E or e**
Displays numeric values in scientific (exponential) format. You can specify the number of decimal places.
Example:
Format: {0:E}
1052.0329112756 -> 1.052033E+003
Format: {0:E2}
-1052.0329112756 -> -1.05e+003
**F or f**
Displays numeric values in fixed format. You can specify the number of decimal places.
Example:
Format: {0:F}
1234.567 -> 1234.57
Format: {0:F3}
1234.567 -> 1234.567
**G or g**
Displays numeric values in general format (the most compact of either fixed-point or scientific notation). You can specify the number of significant digits.
Example:
Format: {0:G}
-123.456 -> -123.456
Format: {0:G2}
-123.456 -> -120
F or f
Displays numeric values in fixed format. You can specify the number of decimal places.
Format: {0:F}
1234.567 -> 1234.57
Format: {0:F3}
1234.567 -> 1234.567
G or g
Displays numeric values in general format (the most compact of either fixed-point or scientific notation). You can specify the number of significant digits.
Format: {0:G}
-123.456 -> -123.456
Format: {0:G2}
-123.456 -> -120
N or n
Displays numeric values in number format (including group separators and optional negative sign). You can specify the number of decimal places.
Format: {0:N}
1234.567 -> 1,234.57
Format: {0:N4}
1234.567 -> 1,234.5670
P or p
Displays numeric values in percent format. You can specify the number of decimal places.
Format: {0:P}
1 -> 100.00%
Format: {0:P1}
.5 -> 50.0%
R or r
Displays Single, Double, or BigInteger values in round-trip format.
Format: {0:R}
123456789.12345678 -> 123456789.12345678
X or x
Displays integer values in hexadecimal format. You can specify the number of digits.
Format: {0:X}
255 -> FF
Format: {0:x4}
255 -> 00ff
Source: Stackoverflow.com