C# date formatting is losing slash separators

前端 未结 3 1246
萌比男神i
萌比男神i 2020-11-30 12:20

If I do this in C#:

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

I would expect output like this:

Wed 6/15/11         


        
相关标签:
3条回答
  • 2020-11-30 12:36
    Console.WriteLine(DateTime.Now.ToString("ffffd M/dd/yy", CultureInfo.InvariantCulture));
                Console.ReadLine();
    

    try the above

    0 讨论(0)
  • 2020-11-30 12:39

    You could also use

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

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

    0 讨论(0)
  • 2020-11-30 12:41

    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 讨论(0)
提交回复
热议问题