DateTime ToString(“dd / MM / yyyy”)返回dd.MM.yyyy [英] DateTime ToString(“dd/MM/yyyy”) returns dd.MM.yyyy
问题描述
我也试图屏蔽格式化字符串中的'/'符号,但它并没有很好的工作。我的最终目标是以'/'符号作为分隔符来获取日期。我想我可以使用 DateTime.ToString(dd / MM / yyyy)替换('。','/')
,但感觉有点过分。 / p>
I have also tried shielding the '/' symbol in the formatting string, but it didn't quite work. My final goal is to get the date with the '/' symbols as separators. I guess I can use DateTime.ToString("dd/MM/yyyy").Replace('.', '/')
, but that feels a bit excessive.
推荐答案
日期/时间格式字符串中的 /
字符代表格式提供者的日期分隔符为。由于您不提供格式提供程序使用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.
这篇关于DateTime ToString(“dd / MM / yyyy”)返回dd.MM.yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!