1

I'm suddenly having problems with the C# DateTime.ToString() method which I have never seen before. I'm working on a project which was working just fine before the weekend and now come Monday, all my DateTime strings are with periods instead of colons (i.e. 2020-12-14 13:45:33 becomes 2020-12-14 13.45.33).

I'm using this format string "yyyy-MM-dd HH:mm:ss" when creating strings from my DateTime objects and it has always worked for me, but now this... What is going on?

This just happened over night and I have no idea as to why...

Anyone has an idea to why this is happening and how to fix it?

EDIT: to answer a few questions in comments - I live in Denmark, OS is Windows 10 x64, framework is .NET 4.7.2.

EDIT 2: I have fixed my problem for now by adding a section to the web.config of my web application, but I still have no idea why this is suddenly required from one day to the other.

I added the following to the system.web section of the web.config:

<globalization culture="en-US" uiCulture="da-DK" />

The uiCulture isn't important in this case, but the culture="en-US" changed the dots back to colons in the time part of the datetime strings as I needed them to be and my web application is back to running normally again...

phuzi
  • 12,078
  • 3
  • 26
  • 50
Aidal
  • 799
  • 4
  • 8
  • 33
  • .NET Framework or .NET Core? What OS are you running on? What country do you live in? – mjwills Dec 14 '20 at 08:22
  • See also https://social.msdn.microsoft.com/Forums/vstudio/en-US/bb926074-d593-4e0b-8754-7026acc607ec/datetime-tostring-colon-replaced-with-period?forum=csharpgeneral – mjwills Dec 14 '20 at 08:24
  • 5
    The symbols in a format specifier aren't *literal* symbols; they're *tokens* that are placeholders for the *actual* value as specified in the `CultureInfo`. It sounds like you're using a culture where the `.DateTimeFormat.TimeSeparator` is period rather than colon, so: check which culture is configured, or: specify an explicit culture in your code if you want to override the ambient one. A quick test shows that the following cultures use `"."` as the time separator: as as-IN da da-DK da-GL en-DK en-FI fi fi-FI id id-ID kl kl-GL si si-LK smn smn-FI – Marc Gravell Dec 14 '20 at 08:25
  • If you want to specify that it should become a dot regardless of the culture, [escape them](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#Literals): `HH\\.mm\\.ss`. – CodeCaster Dec 14 '20 at 08:29
  • Updated question with some more info as per request. Also I do not want a dot, I'm getting a dot instead of colon - From one day to the other this has changed throughout my entire app... – Aidal Dec 14 '20 at 08:34
  • 1
    Probably something in Windows 10 was changed. I usually do this if I want to specify the exact datetime rendering regardless of what the computers regional setting is: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); – nivs1978 Apr 20 '22 at 08:22
  • You can escape the seperator to become what you want, fx as you prefer colon I suggest to use the format `yyyy-MM-dd HH\:mm\:ss` – Muleskinner Jun 24 '22 at 10:47

0 Answers0