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.

toxaq
  • 6,745
  • 3
  • 46
  • 56
  • 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 '09 at 02: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 '09 at 02:39

3 Answers3

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 :)

mlessard
  • 513
  • 6
  • 13
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.

DirkOdb
  • 61
  • 1
  • 2
5

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

if (DateTimeFormatInfo.CurrentInfo.AMDesignator == "")
  //24hour format
else
  //12hour format
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    It's 'DateTimeFormatInfo.CurrentInfo.AMDesignator' but exactly what I need – Anytoe Sep 23 '15 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). – SePröbläm Jun 12 '18 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 '20 at 02:03
  • 2
    @Tommix It was five years ago (and a couple of .NET versions) :) – jgauffin Mar 14 '20 at 12:57