Solved

Find whether a computer is using 24 hour or 12 hour time format in C#

Posted on 2016-09-07
3
40 Views
Last Modified: 2016-10-25
Existing code of my application

   public static string FormatTime()
    {
       return DateTime.UtcNow.ToLocalTime().ToLongTimeString();
    }
This code always returns time in 12hr format even when my computer shows time in 24hr format. I want to show time in my application in same format as computer's clock shows time.
0
Comment
Question by:VG VG
3 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 41789924
By default, the DateTime class uses the localization\globalization settings of the calling thread; e.g. -
using System;

namespace EE_Q28968415
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime now = DateTime.Now;
			DateTime utc = DateTime.UtcNow;

			Console.WriteLine("Short Time Now: {0}", now.ToShortTimeString());
			Console.WriteLine("Short Time Now (UTC): {0}", utc.ToShortTimeString());
			Console.WriteLine("Long Time Now: {0}", now.ToLongTimeString());
			Console.WriteLine("Long Time Now (UTC): {0}", utc.ToLongTimeString());
			Console.WriteLine("Short Date Now: {0}", now.ToShortDateString());
			Console.WriteLine("Short Date Now (UTC): {0}", utc.ToShortDateString());
			Console.WriteLine("Long Date Now: {0}", now.ToLongDateString());
			Console.WriteLine("Long Date Now (UTC): {0}", utc.ToLongDateString());
			Console.ReadLine();
		}
	}
}

Open in new window

For the following settings -Capture.JPGProduces the following output -Capture.JPG
We can override this behaviour by changing our threads culture specific localization/globalization information:
using System;
using System.Globalization;
using System.Threading;

namespace EE_Q28968415
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime now = DateTime.Now;
			DateTime utc = DateTime.UtcNow;

			Console.WriteLine("Short Time Now: {0}", now.ToShortTimeString());
			Console.WriteLine("Short Time Now (UTC): {0}", utc.ToShortTimeString());
			Console.WriteLine("Long Time Now: {0}", now.ToLongTimeString());
			Console.WriteLine("Long Time Now (UTC): {0}", utc.ToLongTimeString());
			Console.WriteLine("Short Date Now: {0}", now.ToShortDateString());
			Console.WriteLine("Short Date Now (UTC): {0}", utc.ToShortDateString());
			Console.WriteLine("Long Date Now: {0}", now.ToLongDateString());
			Console.WriteLine("Long Date Now (UTC): {0}", utc.ToLongDateString());
			Console.WriteLine();
			Console.WriteLine("Let's change the threads culture information -");
			Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-EN");
			Console.WriteLine("Short Time Now: {0}", now.ToShortTimeString());
			Console.WriteLine("Short Time Now (UTC): {0}", utc.ToShortTimeString());
			Console.WriteLine("Long Time Now: {0}", now.ToLongTimeString());
			Console.WriteLine("Long Time Now (UTC): {0}", utc.ToLongTimeString());
			Console.WriteLine("Short Date Now: {0}", now.ToShortDateString());
			Console.WriteLine("Short Date Now (UTC): {0}", utc.ToShortDateString());
			Console.WriteLine("Long Date Now: {0}", now.ToLongDateString());
			Console.WriteLine("Long Date Now (UTC): {0}", utc.ToLongDateString());
			Console.ReadLine();
		}
	}
}

Open in new window

Now produces the following results -Capture.JPG
To combat this issue, you can always just specify the format no matter the regional settings by using the standard ToString() method and explicitly defining your format; e.g. -
public static string FormatTime()
{
	return DateTime.UtcNow.ToLocalTime().ToString("HH:mm:ss");
}

Open in new window


-saige-
0
 
LVL 41

Assisted Solution

by:pcelba
pcelba earned 250 total points
ID: 41789957
It seems saige was faster and my solution is very similar but it is written down... No points please :-)
Note the commented line
            // myDTFI.LongTimePattern = "HH:mm:ss";
which changes the time format for any culture.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.UtcNow.ToLocalTime().ToLongTimeString());
            Console.WriteLine(DateTime.UtcNow.ToLocalTime().ToString("H:mm:ss"));

            CultureInfo culture = CultureInfo.CurrentCulture;
            Console.WriteLine("The current culture is {0} [{1}]",
                              culture.NativeName, culture.Name);

            // Displays the values of the pattern properties.
            
            Console.WriteLine( " CULTURE    PROPERTY VALUE" );
            PrintPattern( "en-US" );
            PrintPattern( "ja-JP" );
            PrintPattern( "fr-FR" );
            PrintPattern(culture.Name);

            Console.ReadLine();
        }
      
        public static void PrintPattern( String myCulture )  
        {
            DateTimeFormatInfo myDTFI = new CultureInfo( myCulture, false ).DateTimeFormat;
            // myDTFI.LongTimePattern = "HH:mm:ss";
            Console.WriteLine( "  {0}     {1}", myCulture, myDTFI.LongTimePattern );
        }

    }
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now