Solved

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

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

Accepted Solution

by:
it_saige earned 250 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

12 Experts available now in Live!

Get 1:1 Help Now