Why doesn't DateTime.ToShortTimeString() respect the Short Time format in “Regional and Language Settings”?

后端 未结 3 1666
梦毁少年i
梦毁少年i 2020-12-31 13:57

I have run into an issue that is probably due to my mis-understanding of how the DateTime.ToShortTimeString() method works. When formatting time strings with this function,

3条回答
  •  执念已碎
    2020-12-31 14:04

    Answer to your second question is

    DateTimeFormat.Format(DateTime.Now, "t", CultureInfo.CurrentUICulture);
    

    or

    DateTime.Now.ToString("t", CultureInfo.CurrentUICulture);
    

    It’s actually always better to use explicit methods that accept CultureInfo. There is no consistency how .Net chooses what to use by default either CurrentCulture or CurrentUICulture or InvarinatCulture.

    To make answer full. Also I will outline differences between cultures.

    So CurrentCulture is "Control Panel -> Clock, Language and Region -> Region and Language -> Formats Tab." This is culture that you expect your calculations to be. For example you can do your accounting in US, so you have this to be configured in US.

    CurrentUICulture is "Region and Language -> Display language", means that when you are emigrant from Ukraine, you want your application to be localized in UA (but all calculations is still in US).

    And InvariantCulture is so called culture agnostic locale. You should use this for storing information and so on. Effectively it En-US.

    Note: I could be wrong where each setting is located in windows. But you probably got an idea.

提交回复
热议问题