0

I want a date format in danish in this format

"Tor. 27/6"

Tor is short for torsdag, meaning thursday in danish

I have this code

string formatted = datetime.ToString("ddd. dd/MM", new System.Globalization.CultureInfo("da-DK"));

It's returning

"To. 27-6"

so it's using a dash and ignoring the slash. How do i force it to use slash while still using cultureinfo danish?

Hans Preutz
  • 679
  • 2
  • 10
  • 28

2 Answers2

3

Yes, the / character has the specific meaning of "culture specific date separator" in custom date/time format strings.

If you want a literal /, you need to quote it in the pattern:

var culture = new CultureInfo("da-DK");
string formatted = datetime.ToString("ddd. dd'/'MM", culture);

Output on my machine:

to. 27/06

It's using "06" instead of "6" because you've used MM in your format string - if you don't want zero padding for the day and month numbers, use "ddd. d'/'M" as the format string. That doesn't help the "to" become "Tor", admittedly. If you want that, you'd need to modify the culture's abbreviated day-of-week values.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • " If you want that, you'd need to modify the culture's abbreviated day-of-week values.", how, can you give example? – Hans Preutz Jun 27 '19 at 09:35
  • @HansPreutz: Not in comments, no. I suggest you ask a new question with all the relevant details. (I'm interested that you get "To." when I get "to." though...) – Jon Skeet Jun 27 '19 at 09:43
  • @Cid: The length would vary based on the day and month number. It's not clear why you expect Hans to need exactly 10 characters. (I can't see that requirement anywhere.) I *expect* Hans just always wants 3 character day names, which is much easier to sort out with a custom culture than manipulating the final result, IMO. – Jon Skeet Jun 27 '19 at 09:47
  • @Cid: Yes, but Hans already expressed a desire for it to *not* be a zero-padded month number. (I want a date format in danish in this format "Tor. 27/6") – Jon Skeet Jun 27 '19 at 09:51
  • @JonSkeet my bad, I didn't notice it in the question. that's definitely not a solution then – Cid Jun 27 '19 at 09:51
  • @JonSkeet, sorry my bad. I did in fact get "to." it was a copy paste error. – Hans Preutz Jun 27 '19 at 11:57
2

surround the desired date seperator ('/' in this case) with single quote.

string formatted = datetime.ToString("ddd. dd'/'MM", new System.Globalization.CultureInfo("da-DK"));
Manak
  • 261
  • 3
  • 13
  • another solution : same like solution in VB6 // except add '@' in front of the string. `string formatted = datetime.ToString(@"ddd. dd\/MM", new System.Globalization.CultureInfo("da-DK"));` – Manak Jun 27 '19 at 09:52