Solved

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

Posted on 2016-09-07
3
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

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 42

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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