C# date formatting is losing slash separators

为君一笑 提交于 2019-11-26 11:28:32

问题


If I do this in C#:

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

I would expect output 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?


回答1:


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

try the above




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6362088/c-sharp-date-formatting-is-losing-slash-separators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!