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?

3
  • This works on my computer; maybe its a localization problem? Commented Dec 5, 2012 at 14:39
  • Which culture are you expecting? Commented Dec 5, 2012 at 14:40
  • 1
    @DavidPfeffer of course it is. And, karaxuna, using an overload of date.ToString() is more efficient than using an overload of string.Format(). Commented Dec 5, 2012 at 15:01

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think your solution is better then setting specific culture
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")); 

1 Comment

And you can learn how to change the culture here.
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/

Comments

2

Simply try

var dateString = date.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("en-GB")); 

3 Comments

I would upvote this for suggesting ToString instead of string.Format, but you neglected to address the key localization issue.
TBH I only just noticed, that ToString() overload also takes IFormatProvider...you learn every day.
Isn't stackoverflow wonderful? Now it's my turn to learn: What's TBH?
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

1 Comment

You need either to escape the escape character ("{0:dd\\/MM\\/yyyy}") or use a verbatim string literal (@"{0:dd\/MM\/yyyy}")
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.

Comments

1

Try this:

var dateString = string.Format("{0:dd/MM/yyyy}", DateTime.Today, new System.Globalization.CultureInfo("en-GB")); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.