C# 如何修复 ASP .NET BoundField (DataFormatString) 中的日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16357582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix date format in ASP .NET BoundField (DataFormatString)?
提问by iceheaven31
I have a dynamic BoundField (for a DetailsView) with the following code:
我有一个带有以下代码的动态 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);
But somehow, it still shows the wrong format: 2013-04-29T18:15:20.270.
但不知何故,它仍然显示错误的格式:2013-04-29T18:15:20.270。
Any way I could fix this for it to show "29/04/2013" instead? Thanks for your help.
有什么办法可以解决这个问题,让它显示“29/04/2013”?谢谢你的帮助。
采纳答案by ShieldOfSalvation
Determine the data type of your data source column, "CreateDate". Make sure it is producing an actual datetime field and not something like a varchar. If your data source is a stored procedure, it is entirely possible that CreateDate is being processed to produce a varchar in order to format the date, like so:
确定数据源列“CreateDate”的数据类型。确保它生成一个实际的日期时间字段,而不是类似 varchar 的东西。如果您的数据源是存储过程,则完全有可能正在处理 CreateDate 以生成 varchar 以格式化日期,如下所示:
SELECT CONVERT(varchar,TableName.CreateDate,126) AS CreateDate
FROM TableName ...
Using CONVERT like this is often done to make query results fill the requirements of whatever other code is going to be processing those results. Style 126 is ISO 8601 format, an international standard that works with any language setting. I don't know what your industry is, but that was probably intentional. You don't want to mess with it. This style (126) produces a string representation of a date in the form '2013-04-29T18:15:20.270' just like you reported! However, if CreateDate's been processed this way then there's no way you'll be able to get your bf1.DataFormatString to show "29/04/2013" instead. You must first start with a datetime type column in your original SQL data source first for bf1 to properly consume it. So just add it to the data source query, and call it by a different name like CreateDate2 so as not to disturb whatever other code already depends on CreateDate, like this:
像这样使用 CONVERT 通常是为了使查询结果满足任何其他代码将要处理这些结果的要求。样式 126 是 ISO 8601 格式,这是一种适用于任何语言设置的国际标准。我不知道你的行业是什么,但这可能是故意的。你不想惹它。这种样式 (126) 以 '2013-04-29T18:15:20.270' 的形式生成日期的字符串表示,就像您报告的那样!但是,如果 CreateDate 以这种方式处理,那么您将无法让 bf1.DataFormatString 显示“29/04/2013”。您必须首先从原始 SQL 数据源中的日期时间类型列开始,以便 bf1 正确使用它。所以只需将其添加到数据源查询中,
SELECT CONVERT(varchar,TableName.CreateDate,126) AS CreateDate,
TableName.CreateDate AS CreateDate2
FROM TableName ...
Then, in your code, you'll have to bind bf1 to "CreateDate2" instead of the original "CreateDate", like so:
然后,在您的代码中,您必须将 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);
Voila! Your date should now show "29/04/2013" instead!
瞧!您的日期现在应该显示为“29/04/2013”!
回答by Soner G?nül
Formatting depends on the server's culture setting. If you use en-USculture, you can use Short Date Patternlike {0:d}
格式取决于服务器的文化设置。如果您使用en-US文化,则可以使用短日期模式,例如{0:d}
For example, it formats 6/15/2009 1:45:30to 6/15/2009
例如,它格式化6/15/2009 1:45:30为6/15/2009
You can check more formats from BoundField.DataFormatString
回答by Ayyappan Sekaran
The following links will help you:
以下链接将帮助您:
In Client side design page you can try this: {0:G}
在客户端设计页面你可以试试这个:{0:G}
OR
或者
You can convert that datetimeformat inside the query itself from the database:
您可以在查询本身内从数据库转换该日期时间格式:
回答by BornToCode
You could add dataformatstring="{0:M-dd-yyyy}" attribute to the bound field, like this:
您可以将dataformatstring="{0:M-dd-yyyy}" 属性添加到绑定字段,如下所示:
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dd-M-yyyy}" />
回答by Lucas Orlando
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.
我有同样的问题,只需要显示shortdate(没有时间),而且需要多语言设置,所以取决于语言,显示dd-mm-yyyy或mm-dd-yyyy。
Finally using DataFormatString="{0:d}, all works fine and show only the date with culture format.
最后使用DataFormatString="{0:d},一切正常,仅显示具有文化格式的日期。
回答by subramanya46
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 -> 3.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
回答by Mohammad
very simple just add this to your bound field DataFormatString="{0: yyyy/MM/dd}"
非常简单,只需将其添加到您的绑定字段 DataFormatString="{0: yyyy/MM/dd}"

