开发者

How to format a date with slashes in C# [duplicate]

This question already has answers here: why does DateTime.ToString("dd/MM/yyyy") give me dd-MM-yyyy? (6 answers) Closed 5 years ago.

When i write a date in C# by using

开发者_运维技巧DateTime.Now.ToString("yyyy/MM/dd")

then it returns 2010-09-10, but I need 2010/09/10. How do I make it output slashes?


Use

DateTime.Now.ToString("yyyy'/'MM'/'dd");

/ - the date separator. It will be replaced according current culture. So you need enclose it with char literal delimiter (') to use it like char.

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#dateSeparator


Specify a culture. Your current culture uses - for the separators, and that's what ToString defaults to (your current culture), unless you override it.

You can try this:

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.


Another way is to specify the slashes as character literals:

DateTime.Now.ToString("yyyy'/'MM'/'dd");
"2010/09/10"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜