29

我有一个带有以下代码的动态BoundField(用于DetailsView):

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);

但不知何故,它仍然显示错误的格式:2013-04-29T18:15:20.270。

有什么办法可以解决这个问题,让它显示“29/04/2013”​​吗?谢谢你的帮助。

4

7 回答 7

41

您可以将dataformatstring="{0:M-dd-yyyy}" 属性添加到绑定字段,如下所示:

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

来源:无法使用 dataformatstring 格式化日期时间

于 2013-10-13T22:13:52.820 回答
13

格式取决于服务器的文化设置。如果您使用en-US文化,则可以使用短日期模式,例如{0:d}

例如,它格式化6/15/2009 1:45:306/15/2009

您可以从以下位置检查更多格式BoundField.DataFormatString

于 2013-05-03T11:22:30.920 回答
11

确定数据源列的数据类型“CreateDate”。确保它正在生成一个实际的日期时间字段,而不是像 varchar 之类的东西。如果您的数据源是存储过程,则完全有可能正在处理 CreateDate 以生成 varchar 以格式化日期,如下所示:

SELECT CONVERT(varchar,TableName.CreateDate,126) AS CreateDate 
FROM TableName ...

像这样使用 CONVERT 通常是为了使查询结果满足任何其他代码将要处理这些结果的要求。Style 126 是 ISO 8601 格式,一种适用于任何语言设置的国际标准。我不知道你的行业是什么,但这可能是故意的。你不想惹它。就像您报告的那样,这种样式 (126) 会以 '2013-04-29T18:15:20.270' 的形式生成日期的字符串表示形式!但是,如果以这种方式处理 CreateDate,那么您将无法让 bf1.DataFormatString 改为显示“29/04/2013”​​。您必须首先从原始 SQL 数据源中的 datetime 类型列开始,bf1 才能正确使用它。所以只需将其添加到数据源查询中,

SELECT CONVERT(varchar,TableName.CreateDate,126) AS CreateDate, 
       TableName.CreateDate AS CreateDate2
FROM TableName ...

然后,在您的代码中,您必须将 bf1 绑定到“CreateDate2”而不是原始的“CreateDate”,如下所示:

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

dv.Fields.Add(bf1);

瞧!您的日期现在应该显示“29/04/2013”​​!

于 2017-09-20T16:07:39.500 回答
6

我有同样的问题,只需要显示短日期(没有时间),而且需要有多语言设置,所以取决于语言,显示 dd-mm-yyyy 或 mm-dd-yyyy。

最后使用DataFormatString="{0:d},一切正常,只显示文化格式的日期。

于 2016-01-27T13:58:09.200 回答
4

非常简单,只需将其添加到绑定字段 DataFormatString="{0: yyyy/MM/dd}"

于 2017-10-08T07:10:29.997 回答
1

以下链接将为您提供帮助:

在客户端设计页面中,您可以尝试 {0:G}

或者

您可以在数据库中的查询本身内转换该日期时间格式:

于 2013-05-03T11:25:19.280 回答
1
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
于 2017-08-24T07:40:37.847 回答