如何格式化C#中斜线日期 [英] How to format a date with slashes in C#
本文介绍了如何格式化C#中斜线日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用C#编写的日期
When i write a date in C# by using
DateTime.Now.ToString("yyyy/MM/dd")
然后返回 2010-09-10
,但我需要 2010/09/10
。我如何使它输出斜线?
then it returns 2010-09-10
, but I need 2010/09/10
. How do I make it output slashes?
推荐答案
指定的文化。 。您当前的文化用途 - 为分隔符,而这正是的ToString默认(当前的文化),除非你将其覆盖
Specify a culture. Your current culture uses - for the separators, and that's what ToString defaults to (your current culture), unless you override it.
您可以试试这个:
DateTime.Now.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture)
但也许如果你想美国文化它将如果你指定了不同的文化,比如更好的:
but perhaps it would be better if you specified a different culture, for instance if you want the US culture:
DateTime.Now.ToString("yyyy/MM/dd", CultureInfo.GetCultureInfo("en-US"))
上面两种情况,会给你/作为分隔符。
Both of the above will give you / as a separator.
这篇关于如何格式化C#中斜线日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文