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?
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.DateSeparatorproperty 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.
// 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")); 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/
Simply try
var dateString = date.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("en-GB")); ToString() overload also takes IFormatProvider...you learn every day.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); "{0:dd\\/MM\\/yyyy}") or use a verbatim string literal (@"{0:dd\/MM\/yyyy}")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.
date.ToString()is more efficient than using an overload ofstring.Format().