日期时间的ToString(“DD / MM / YYYY”)返回DD.MM.YYYY [英] DateTime ToString(“dd/MM/yyyy”) returns dd.MM.yyyy

查看:161
本文介绍了日期时间的ToString(“DD / MM / YYYY”)返回DD.MM.YYYY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也试图屏蔽/符号格式化字符串,但它确实不是那么回事。我的最终目标是让用/符号作为分隔符的日期。我想我可以使用则DateTime.ToString(DD / MM / YYYY)。更换('。','/'),但感觉有点过分了。


解决方案

在日期/时间格式字符串 / 字符表示无论格式提供的日期分隔符是。既然你不提供格式提供 Thread.CurrentCulture 被使用,并在你的情况下,当前文化使用作为日期分隔符。

如果您想使用的文字的斜线,将它放在单引号内:

 则DateTime.ToString(日日'/'MM'/'YYYY);

另外,你可以在那里的日期分隔符是 / 指定格式提供商

 则DateTime.ToString(DD / MM / YYYY,CultureInfo.InvariantCulture);

以上全部是记录在MSDN上

见一个活生生的例子的区别

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.

解决方案

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.

这篇关于日期时间的ToString(“DD / MM / YYYY”)返回DD.MM.YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆