Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 86
  • Last Modified:

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

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
VG VG
Asked:
VG VG
2 Solutions
 
it_saigeDeveloperCommented:
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
 
pcelbaCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now