57

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?

Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
  • 1
    I get the slashes with your format string. What is the culture you are running under? – Oded Jun 15 '11 at 17:59
  • I copied your code and ran it with Snippet Compiler. It output 'Wed 6/15/11'. – Dave Ferguson Jun 15 '11 at 17:59
  • This issue only appears to happen in Windows 7 when you change your short date format to "ddd M/dd/yy". – Jon Tackabury Jun 15 '11 at 18:01
  • 1
    Nothing to do with Windows 7 and everything to do with the _Culture_ you are running the program under. – Oded Jun 15 '11 at 18:08
  • Is there a way to implement this using string.Format()? The slashes are replaced when I use `string.Format("{0:M/d/yyyy H:mm:ss tt}", myDate)` and `string.Format("{0:G}", myDate)` – JackAce Mar 07 '17 at 17:36

3 Answers3

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

try the above

David
  • 4,185
  • 2
  • 27
  • 46
  • 3
    This appears to work, why is the CultureInfo.InvariantCulture required here? – Jon Tackabury Jun 15 '11 at 18:04
  • 16
    @Jon - Because if a `CultureInfo` is not specified, the current culture will be used. If this is a culture that doesn't use slashes as separators in dates (and the format string specifies the date separator to be a `/`, that is replaced by whatever the actual culture date separator is). – Oded Jun 15 '11 at 18:07
  • 1
    based on http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx I would assume that it appears that your cultureinfo is changing the "/" to " " – David Jun 15 '11 at 18:08
  • My Default culture considers dash as default date separator, even if Region Format to set to uses slash "/" as default date separator, which is English (US). Am I missing anything ? Please suggest I am using windows 11, Timezone IST +5:30. – Himen Suthar Apr 05 '23 at 09:05
32

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.

Métoule
  • 13,062
  • 2
  • 56
  • 84
BaRtEr
  • 321
  • 3
  • 2
  • Very nice detail :) using your solution in conjunction with "Culture.Invariant" will guarantee at 100% that your date has the exact format that you specified :) – Pedro Simões May 30 '16 at 15:31
32

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.

Norbert
  • 4,239
  • 7
  • 37
  • 59