detecting 12 or 24 hour support for DateTime

  • Thread starter Thread starter freddyboy
  • Start date Start date
F

freddyboy

Hi

I'm trying to detect if the current culture support 12h or 24h?

I need to change string values based on the hour formatting.

I tried to
DateTime dt = new DateTime(1,1,1,14,0,0,0);
string s = string.Format("{0}:00", dt.Hour.ToString()); //

I want s to output 14:00 when using 24h and 2:00 when using 12h

right now when i change my time regional options, to h or H , I always
get "14:00"

how can i do this ?

thanks
 
hello freddyboy

System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern
contains the value from the regional settings ([..].ShortTimePattern
always defaults to the system LCID setting).

Use something like this:

if([..].LongTimePattern.StartsWith("h"))
myString = myDate.ToString("hh:mm")
else
myString = myDate.ToString("HH:mm")

I know it's not accurate, depending on what you intend to do with the
string, some additional checkings may be required.

If the system LCID setting is enough for you, just use
myDate.ToShortDateString()


Hope this helps

Best Regards
Philipp Fabrizio
 
Back
Top