为什么这两种日期格式不同?

时间:2023-01-14 16:10:26

I'm trying to produce just the day number in a WPF text block, without leading zeroes and without extra space padding (which throws off the layout). The first produces the day number with a space, the second produces the entire date. According to the docs, 'd' should produce the day (1-31).

我正在尝试在WPF文本块中生成日期编号,没有前导零并且没有额外的空间填充(这会抛弃布局)。第一个产生带有空格的日期编号,第二个产生整个日期。根据文档,'d'应该产生一天(1-31)。

string.Format("{0:d }", DateTime.Today);
string.Format("{0:d}", DateTime.Today);

UPDATE:Adding % is indeed the trick. Appropriate docs here.

更新:添加%确实是诀窍。适当的文档在这里。

3 个解决方案

#1


5  

See here

d, %d

The day of the month. Single-digit days do not have a leading zero. The application specifies "%d" if the format pattern is not combined with other format patterns.

这个月的哪一天。单位数天没有前导零。如果格式模式未与其他格式模式组合,则应用程序指定“%d”。

Otherwise d is interpreted as:

否则d被解释为:

d - 'ShortDatePattern'

d - 'ShortDatePattern'

PS. For messing around with format strings, using LinqPad is invaluable.

PS。对于搞乱格式字符串,使用LinqPad是非常宝贵的。

#2


1  

From the MSDN documentation for "Custom Date and Time Format Strings":

从“自定义日期和时间格式字符串”的MSDN文档:

Any string that is not a standard date and time format string is interpreted as a custom date and time format string.

任何非标准日期和时间格式字符串的字符串都将被解释为自定义日期和时间格式字符串。

{0:d} is interpreted as a standard data and time format string. From "Standard Date and Time Format Strings", the "d" format specifier:

{0:d}被解释为标准数据和时间格式字符串。从“标准日期和时间格式字符串”,“d”格式说明符:

Represents a custom date and time format string defined by the current ShortDatePattern property.

表示由当前ShortDatePattern属性定义的自定义日期和时间格式字符串。

With the space, {0:d } doesn't match any standard date and time format string, and is interpreted as a custom data and time format string. From "Custom Date and Time Format Strings", the "d" format specifier:

对于空格,{0:d}与任何标准日期和时间格式字符串都不匹配,并被解释为自定义数据和时间格式字符串。从“自定义日期和时间格式字符串”,“d”格式说明符:

Represents the day of the month as a number from 1 through 31.

将月中的日期表示为1到31之间的数字。

#3


0  

The {0:d} format uses the patterns defined in the Standard Date and Time Format Strings document of MSDN. 'd' translates to the short date pattern, 'D' to the long date pattern, and so on and so forth.

{0:d}格式使用MSDN的标准日期和时间格式字符串文档中定义的模式。 'd'转换为短日期模式,'D'转换为长日期模式,依此类推。

The format that you want appears to be the Custom Date and Time Format Modifiers, which work when there is no matching specified format (e.g., 'd ' including the space) or when you use ToString().

您想要的格式似乎是自定义日期和时间格式修饰符,当没有匹配的指定格式(例如,'d'包括空格)或使用ToString()时,它可以工作。

You could use the following code instead:

您可以使用以下代码:

string.Format("{0}", DateTime.Today.ToString("d ", CultureInfo.InvariantCulture));

#1


5  

See here

d, %d

The day of the month. Single-digit days do not have a leading zero. The application specifies "%d" if the format pattern is not combined with other format patterns.

这个月的哪一天。单位数天没有前导零。如果格式模式未与其他格式模式组合,则应用程序指定“%d”。

Otherwise d is interpreted as:

否则d被解释为:

d - 'ShortDatePattern'

d - 'ShortDatePattern'

PS. For messing around with format strings, using LinqPad is invaluable.

PS。对于搞乱格式字符串,使用LinqPad是非常宝贵的。

#2


1  

From the MSDN documentation for "Custom Date and Time Format Strings":

从“自定义日期和时间格式字符串”的MSDN文档:

Any string that is not a standard date and time format string is interpreted as a custom date and time format string.

任何非标准日期和时间格式字符串的字符串都将被解释为自定义日期和时间格式字符串。

{0:d} is interpreted as a standard data and time format string. From "Standard Date and Time Format Strings", the "d" format specifier:

{0:d}被解释为标准数据和时间格式字符串。从“标准日期和时间格式字符串”,“d”格式说明符:

Represents a custom date and time format string defined by the current ShortDatePattern property.

表示由当前ShortDatePattern属性定义的自定义日期和时间格式字符串。

With the space, {0:d } doesn't match any standard date and time format string, and is interpreted as a custom data and time format string. From "Custom Date and Time Format Strings", the "d" format specifier:

对于空格,{0:d}与任何标准日期和时间格式字符串都不匹配,并被解释为自定义数据和时间格式字符串。从“自定义日期和时间格式字符串”,“d”格式说明符:

Represents the day of the month as a number from 1 through 31.

将月中的日期表示为1到31之间的数字。

#3


0  

The {0:d} format uses the patterns defined in the Standard Date and Time Format Strings document of MSDN. 'd' translates to the short date pattern, 'D' to the long date pattern, and so on and so forth.

{0:d}格式使用MSDN的标准日期和时间格式字符串文档中定义的模式。 'd'转换为短日期模式,'D'转换为长日期模式,依此类推。

The format that you want appears to be the Custom Date and Time Format Modifiers, which work when there is no matching specified format (e.g., 'd ' including the space) or when you use ToString().

您想要的格式似乎是自定义日期和时间格式修饰符,当没有匹配的指定格式(例如,'d'包括空格)或使用ToString()时,它可以工作。

You could use the following code instead:

您可以使用以下代码:

string.Format("{0}", DateTime.Today.ToString("d ", CultureInfo.InvariantCulture));