12

I'm modifying a globalized web application which uses stored CultureInfo for each logged in user.

The client would like time data entry to be localized. Displaying is not a problem as the formatting is already available. However I need to detect if the current cultureinfo is for 24 hour time or am/pm so I can display the correct input boxes (not just a textfield).

My initial idea was to check the DateTimeInfo property of CultureInfo and see if the ShortTimePattern contained a capital H or a lower case h but this didn't feel robust enough for me.

Is there a better way? I've read the class properties of both but unless I'm missing something, I can't see any existing methods or properties.

2
  • Thanks @mlessard - Checking for an escaped character is a good idea. I probably won't do it at this stage as we're not using custom cultures, it's all pretty straight forward stuff.
    – toxaq
    Jul 16, 2009 at 2:38
  • This is the code I've used to create this functionality as an extension method: using System; using System.Globalization; namespace My.Extensions { public static class CultureInfoExtensions { public static bool Is24HourTime(this CultureInfo culture) { return culture.DateTimeFormat.ShortTimePattern.Contains("H"); } } }
    – toxaq
    Jul 16, 2009 at 2:39

3 Answers 3

8

I don't think there is a better way to obtain that information. The time pattern for a culture could contain anything (a user could even create a custom culture where the ShortTimePattern is "\hello" and then DateTime.ToString() would return "hello" for any time). In that case how could the framework determine if that CultureInfo is in 24-hour or 12-hour format?

So a "normal" DateTimeFormatInfo.ShortTimePattern will necessarily contain either a 'h' or a 'H', otherwise the hour will not be displayed. I think you can follow your initial idea and check for that. You can also check that the 'h' or 'H' is not escaped with a \ like in my "\hello" example because that would not represent the hour :)

6

Checking for 'H'/'h' seems more robust than checking for the AM/PM Designator. A good example is en-gb: The time format string is HH:mm and the AM/PM designators are set to AM/PM Windows will display the time in 24h format! This seems to be an inconsistent definition but checking for 'H' fixed my bug.

5

The most robust way is to check if DateTimeFormatInfo.AMDesignator is an empty string.

if (DateTimeFormatInfo.CurrentInfo.AMDesignator == "")
  //24hour format
else
  //12hour format
4
  • 1
    It's 'DateTimeFormatInfo.CurrentInfo.AMDesignator' but exactly what I need
    – Anytoe
    Sep 23, 2015 at 15:20
  • Thank you for sharing this! Just as a side note: This unfortunately doesn't work in Unity, where 'DateTimeFormatInfo.CurrentInfo.AMDesignator' contains "AM" even if the clock is set to 24 hours time format (Mac OSX). Jun 12, 2018 at 18:10
  • 1
    I DO NOT agree with this answer. I just opened format settings in windows and as AM and PM symbol choose AM and PM and saved. Your code now gives me AM as a result, but system is still using 24h format. Of course i did it manually (AM PM) so you my argue that that doesn't count..but.. i dont agree :) It can't be called most robust way.
    – Tommix
    Mar 14, 2020 at 2:03
  • 2
    @Tommix It was five years ago (and a couple of .NET versions) :)
    – jgauffin
    Mar 14, 2020 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.