22

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?

Timwi
  • 65,159
  • 33
  • 165
  • 230

3 Answers3

32

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

ProfK
  • 49,207
  • 121
  • 399
  • 775
Andrey
  • 727
  • 6
  • 12
29

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.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
9

Another way is to specify the slashes as character literals:

DateTime.Now.ToString("yyyy'/'MM'/'dd");
"2010/09/10"
stuartd
  • 70,509
  • 14
  • 132
  • 163