0

I have this code:

var ci = new CultureInfo("it-IT", false);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

var format = "HH:mm\\:ssZ";
var formatted = DateTime.Now.ToString( format, ci );

and... formatted happens to be 09:08:32Z on my machine which has default culture different from "Italy". MSDN says that colon is a format specifier and provides a sample of how an unescaped colon is formatted as a dot with "it-IT" culture (the one I have in the code above).

In my code I have one colon character escaped and one unescaped and they are both formatted as a colon.

Why does it happen? Why is thread current culture seemingly being ignored?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 1
    Did you check the value of `ci.DateTimeFormat.TimeSeparator`? What is its value? – Martin Mulder Nov 13 '15 at 09:20
  • I tried your code on .NET 4.5 and it shows `:` as a `TimeSeparator` for `it-IT` culture. I don't see anything wrong with your code, honestly. If you set `ci.DateTimeFormat.TimeSeparator = "."`, your result will be `09.08:32Z` on .NET 4.5 version. I didn't look the older versions of it but these things _can_ be change over .NET or OS versions. – Soner Gönül Nov 13 '15 at 09:25
  • Seems related: [.NET (3.5) formats times using dots instead of colons as TimeSeparator for it-IT culture?](http://stackoverflow.com/q/7498058). – user4003407 Nov 13 '15 at 09:34

1 Answers1

1

The time separator for "it-IT" culture is a colon, as you can see if you check ci.DateTimeFormat.TimeSeparator. I guess that the MSDN documentation is wrong.

You can always manually change the time separator (ci.DateTimeFormat.TimeSeparator = ".") if you really need it.

EDIT: As stated in comments, it seems that the time separator for the it-IT culture was a dot in .NET 3.5, but was changed to a colon in .NET 4.0.

Community
  • 1
  • 1
Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • On which part MSDN is wrong exactly? I couldn't catch your point. – Soner Gönül Nov 13 '15 at 09:27
  • MSDN shows this example: `2009-06-15T13:45:30 -> . (it-IT)`. So I guess they actually meant `: (it-IT)` – Konamiman Nov 13 '15 at 09:32
  • Interesting. It all started with my ASP.NET service getting requests with `Accept-Language: it-IT` and formatting time with dots. – sharptooth Nov 13 '15 at 09:35
  • @Konamiman Oh I see. Yes, you are right about that. Since the document based on .NET Framework 4.6 and 4.5 versions, it should `:`, not `.`. – Soner Gönül Nov 13 '15 at 09:41
  • @sharptooth Looks like this `TimeSeparator` of _that_ culture [changed after .NET 3.5 version](http://stackoverflow.com/q/7498058/447156). Since you tagged with `.net-4-5`, it is normal to see it as `:`. – Soner Gönül Nov 13 '15 at 09:43
  • @SonerGönül It may so happen that somehow .NET framework 3.5 is used for some pieces of the actual production code and that's why we see dots there. – sharptooth Nov 13 '15 at 09:45