且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DateTime ToString(“dd / MM / yyyy”)返回dd.MM.yyyy

更新时间: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.