更新时间:2023-02-26 20:43:32
日期/时间格式字符串中的 /
字符代表格式提供者的日期分隔符为。由于您不提供格式提供程序使用Thread.CurrentCulture
,而在您的情况下,当前文化使用。
as日期分隔符。
The /
character in date/time format strings stands for "whatever the date separator of the format provider is". Since you do not supply a format provider Thread.CurrentCulture
is used, and in your case the current culture uses .
as the date separator.
如果要使用文字斜杠,请将其放在单引号内:
If you want to use a literal slash, place it inside single quotes:
dateTime.ToString("dd'/'MM'/'yyyy");
或者,您可以指定格式提供程序,其中日期分隔符为 /
:
Alternatively, you could specify a format provider where the date separator is /
:
dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
All of the above is documented on MSDN.
See the difference in a live example.