开发者

C# date formatting is losing slash separators

If I do this in C#:

Console.WriteLine(DateTime.Now.ToString("ddd M/dd/yy"));

I would expect out开发者_运维知识库put like this:

Wed 6/15/11

But it actually outputs this:

Wed 6 15 11

Why are the slashes disappearing? Is there a way to prevent this and have the date outputted in the expected format?


Console.WriteLine(DateTime.Now.ToString("ddd M/dd/yy", CultureInfo.InvariantCulture));
            Console.ReadLine();

try the above


You could also use

Console.WriteLine(dateTime.ToString("ddd M'/'dd'/'yy"));

That's a possible solution if you're not using the invariant culture as mentioned in other answers here.


The default behavior of the "/" (slash) in a format argument is to use the current's culture date separator.

To force the "/" (slash), you must precede it with a "\" (backslash).

Ex.: "yyyy\\/MM\\/dd" will always display a date like "2015/07/02" independent of the current culture in use.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜