0

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nomesh Gajare
  • 855
  • 2
  • 12
  • 28
  • 1
    What are you asking? Please show some code of what you are trying to accomplish. The '/' character is a format specifier like m,M,D etc and doesn't change no matter what culture you use – Panagiotis Kanavos Jul 11 '13 at 07:17
  • It's hard to tell what you want without seeing your code, but do you ask how to use "/" in date format strings so that they don't get replaced by culture specific date seperators? If yes, the answer is to place single quotes around them in the format string, e.g. `date.ToString("yyyy'/'MM'/'dd")` – cremor Jul 11 '13 at 07:31
  • Please give an example of the ShortDatePattern you want to use and the resulting string. I think what you ask is impossible though, because `/` is part of the format string's syntax and is not culture-specific – Panagiotis Kanavos Jul 11 '13 at 08:17
  • i think the best way to do this is by changing my customCulture.DateTimeFormat.DateSeparator = "/". It works because if the user format contains "/" it will be replaced by "/" itself. – Nomesh Gajare Jul 11 '13 at 09:14

4 Answers4

3

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 = "/";
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • That's not what's asked. The OP asks how to change the '/' format specifier to something else, not how th change the separator used when '/' is encountered – Panagiotis Kanavos Jul 11 '13 at 07:21
  • 1
    @PanagiotisKanavos: As far as I understand, that *is* what is asked. See his comment on MarcinJuraszek's answer. He wants it to work like `CultureInfo.InvariantCulture`. This culture has a date separator of `/`. If you have deeper insight into what is asked, you are free to provide your own answer. – Daniel Hilgarth Jul 11 '13 at 07:25
  • I don't think it's clear what's asked: Either change the function of the format specifier which isn't possible, or change the date separator which is a no-brainer. – Panagiotis Kanavos Jul 11 '13 at 07:29
  • So, its the impossible question. It's not about changing the separator, it's how to change the separator specifier character of the format string – Panagiotis Kanavos Jul 11 '13 at 08:05
  • @PanagiotisKanavos: My answer would still give the expected result, i.e. what he wants to achieve in the end: A date with the slashes. – Daniel Hilgarth Jul 11 '13 at 08:06
1

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

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • no i dont want to use escape character! i want my customCulture to work as CultureInfo.InvariantCulture. – Nomesh Gajare Jul 11 '13 at 07:14
  • 2
    '/' is a special format specifier that specifies where to put a culture's defined date separator. It is NOT affected by the culture. Using the escape character is the only option – Panagiotis Kanavos Jul 11 '13 at 07:20
1

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

Joe
  • 122,218
  • 32
  • 205
  • 338
1

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);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236