Solved

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

Posted on 2016-09-07
3
45 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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