PC Review


Reply
Thread Tools Rate Thread

detecting 12 or 24 hour support for DateTime

 
 
freddyboy
Guest
Posts: n/a
 
      21st Dec 2006
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

 
Reply With Quote
 
 
 
 
Philipp
Guest
Posts: n/a
 
      21st Dec 2006
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






freddyboy schrieb:

> 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


 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      22nd Dec 2006
myDate.ToShortTimeString(), not ToShortDateString of course

 
Reply With Quote
 
freddyboy
Guest
Posts: n/a
 
      3rd Jan 2007
Thanks this is exactly what I was looking for

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting database column data type of smalldatetime vs. datetime? Jen Microsoft ADO .NET 3 8th Aug 2007 08:44 AM
detecting 12 or 24 hour support for DateTime freddyboy Microsoft Dot NET Framework 3 3rd Jan 2007 04:27 PM
.NET 2.0: Fraction support broken for DateTime? sumip@freesurf.ch Microsoft C# .NET 4 4th Jul 2006 07:56 PM
Detecting Client's scripting support Austin Rathe Microsoft ASP .NET 3 21st Oct 2004 09:30 AM
DateTime.ToShortTimeString and 24 hour time Robert Misiak Microsoft C# .NET 7 18th May 2004 10:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:41 AM.