2

I have this:

var dateString = string.Format("{0:dd/MM/yyyy}", date);

But dateString is 13.05.2011 instead of 13/05/2011. Can you help me?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
karaxuna
  • 26,752
  • 13
  • 82
  • 117

7 Answers7

5
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2012 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2012 16:05:07" - german (de-DE)

so you have to change Culture from German to English!

you can write :

date.ToString(new CultureInfo("en-EN"));
Obama
  • 2,586
  • 2
  • 30
  • 49
  • And you can learn how to change the culture [here](http://msdn.microsoft.com/en-us/library/bz9tc508(v=vs.100).aspx). – Cᴏʀʏ Dec 05 '12 at 14:43
5

You could use DateTime.ToString with CultureInfo.InvariantCulture instead:

var dateString = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

The reason why / is replaced with . is that / is a custom format specifier

The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfo.DateSeparator property of the current or specified culture.

So either use InvariantCulture which uses / as date separator or - more appropriate - escape this format specifier by embedding it within ':

var dateString = date.ToString("dd'/'MM'/'yyyy");

Why this is more appropriate? Because you can still apply the local culture, f.e. if you want to output the month names, but you force / as date separator anyway.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

try this:

  var dateString = string.Format("{0:dd}/{0:MM}/{0:yyyy}", date);

Also check out Steve X's site for string formatting: http://blog.stevex.net/string-formatting-in-csharp/

EdChum
  • 376,765
  • 198
  • 813
  • 562
RayB64
  • 149
  • 1
  • 4
2

If you want to force the date separator regardless of culture you can escape it, like this:

var dateString = string.Format(@"{0:dd\/MM\/yyyy}", date);

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • 1
    You need either to escape the escape character (`"{0:dd\\/MM\\/yyyy}"`) or use a verbatim string literal (`@"{0:dd\/MM\/yyyy}"`) – phoog Dec 05 '12 at 14:56
2

Simply try

var dateString = date.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("en-GB"));
StaWho
  • 2,488
  • 17
  • 24
  • I would upvote this for suggesting ToString instead of string.Format, but you neglected to address the key localization issue. – phoog Dec 05 '12 at 14:58
  • TBH I only just noticed, that `ToString()` overload also takes `IFormatProvider`...you learn every day. – StaWho Dec 05 '12 at 15:09
  • 1
    Isn't stackoverflow wonderful? Now it's my turn to learn: What's TBH? – phoog Dec 05 '12 at 15:24
1

It seems a date seperator problem. Use this;

String.Format("{0:d/M/yyyy}", date);

Check String Format DateTime and look at DateTimeFormatInfo.DateSeperator property.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

Try this:

var dateString = string.Format("{0:dd/MM/yyyy}", DateTime.Today, new System.Globalization.CultureInfo("en-GB"));
Barry Kaye
  • 7,682
  • 6
  • 42
  • 64