detecting 12 or 24 hour support for DateTime

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
 
P

Philipp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top