在c sharp中使用string.Format [英] Use of string.Format in c sharp
问题描述
请解释以下代码。这里dra是临时表的一行。我可以使用convert,cast或parse方法而不是string.Format。如果可能的话,请举例说明。
Please explain the following code. Here dra is row of a temp table. Can I use convert, cast or parse method instead of string.Format. If possible please explain with example.
DateTime datt = Convert.ToDateTime(DTLocalSc.Rows[0][4]);
dra["dat"] = string.Format("{0:dd-MM-yyyy}", datt);
推荐答案
要解释的是什么?
从控件或数组中获取字符串日期,将其转换为DateTime值,然后将其格式化为显示为日 - 月 - 年。您之前可能没有看过的唯一一点是在string.Format字符串中使用格式化表达式:
What''s to explain?
It gets a string date from a control, or array, converts it to a DateTime value, then formats it for the display as day-month-year. The only bit that you might not have seen before is the use of a formatting expression in a string.Format string:
{0:dd-MM-yyyy}
0数字将提供信息的参数
冒号指定格式字符串如下
dd-MM-yyyy部分指定实际用于DateTime值的格式。
这里有一个DateTime格式字符串的可能值列表:格式化日期时间以显示格式字符串描述 [ ^ ]
the 0 numbers the parameter that will supply the info
The colon specifies a format string is following
The dd-MM-yyyy part specifies the actual format to use for the DateTime value.
There is a list of the possible values for the DateTime formatting string here: Formatting a DateTime for display - format string description[^]
如果dat
字段的类型是字符串(例如varchar
),那么你必须使用的String.Format
。我强烈建议你阅读文档 [ ^ ]。
If the type ofdat
field is string (e.g.varchar
) then you have to useString.Format
. I strongly suggest you reading the documentation[^].
这篇关于在c sharp中使用string.Format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!