Question

What property in CultureInfo.InvariantCulture helps to discard the functionality of "/".

I am having this problem because I have my customCulture. and when I use it to format DateTime it replaces "/" with the culture Specific separator.

So I want to know what property if I change in my customCulture the functionality of "/" will be discarded.

Let me clear my problem,

I want to export some data to a file with DateTime as specified by user.

I am creating customCulture as follows

CultureInfo customCulture =  new CultureInfo(CultureInfo.CurrentCulture.Name);

I am taking the date format from the user and injecting it into my CustomCulture object as

customCulture.DateTimeFormat.ShortDatePattern = MyClass.DateFormatFromUser;

Now when I want to convert this to String pattern. I am doing it as follows,

string.Format(customCulture, "{0:G}", dateTimeValue);

Now the problem is String.Format replaces any "/" present in ShortDatePattern with customCulture.DateTimeFormat.DateSeparator

For example if he user gives the format as

dd/MM/yyyy

and my current culture is faeroese ie., {fo-FO}

For these I get output as 25-09-1990.

Était-ce utile?

La solution

If you want to change your custom culture so that the date separator is the slash (/) then you are looking for the DateTimeFormatInfo.DateSeparator property:

customCulture.DateTimeFormat.DateSeparator = "/";

Autres conseils

Use the escape character \: change every / into \/ when you don't want it to be replaced with culture separator.

DateTimeFormatInfo.DateSeparator is the culture-specific separator that separates the components of a date.

What you ask is not possible and Marcin Juraszek is right. There is a misunderstanding of how the format specifier and the CultureInfo work together.

The / character is a format specifier just like m,D,y,Y and all the other date format specifiers. That's just the syntax of the format string, it has nothing to do with culture and can't change. You can't change the meaning of '/' any more than you can change the meaning of the other specifiers

String.Format uses this specifier to find where it should place the CultureInfo's DateSeparator character when formatting a date.

It doesn't matter whether you include the specifier directly in a format string like String.Format("{0:dd/MM/yyyy}",DateTime.Now) or you pass it indirectly through CultureInfo.DateTimeFormat.ShortDatePattern like you tried, it will always be replaced by the DateSeparator character.

Your only option is to escape the character either by prepending it with '\' or surrounding it with ' ' as cremor suggested, eg.

customCulture.DateTimeFormat.ShortDatePattern = "dd\\/MM/yyyy";

or

customCulture.DateTimeFormat.ShortDatePattern = "dd'/'MM/yyyy";

So, if you wanted to create a string like 11-07/2013 you would have to write something like this:

CultureInfo customCulture = new CultureInfo(CultureInfo.CurrentCulture.Name);
customCulture.DateTimeFormat.ShortDatePattern = "dd/MM\\/yyyy";
customCulture.DateTimeFormat.DateSeparator = "-";
var dateTimeValue = DateTime.Now;
var result = string.Format(customCulture,"{0:d}", dateTimeValue);
Console.WriteLine(result);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top