Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 325129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:11:17+00:00 2026-05-12T09:11:17+00:00

I have run into an issue that is probably due to my mis-understanding of

  • 0

I have run into an issue that is probably due to my mis-understanding of how the DateTime.ToShortTimeString() method works. When formatting time strings with this function, I was assuming that it would respect the “Short Time” setting in Windows 7’s Format settings

Control Panel -> Clock, Language and Region -> Region and Language -> Formats Tab.

However .NET seems to select a short time format not based upon this setting but based upon the current culture:

Region and Language -> Location -> Current Location

I did some testing on Windows 7 RC:

Culture: en-GB, 6AM: 06:00, 6PM: 18:00 // HH:mm (United Kingdom)
Culture: en-GB, 6AM: 06:00, 6PM: 18:00 // hh:mm (United Kingdom)
Culture: en-US, 6AM: 6:00 AM, 6PM: 6:00 PM // HH:mm (United States)
Culture: en-US, 6AM: 6:00 AM, 6PM: 6:00 PM // hh:mm (United States)
Culture: el-GR, 6AM: 6:00 πμ, 6PM: 6:00 μμ // HH:mm (Greece)
Culture: el-GR, 6AM: 6:00 πμ, 6PM: 6:00 μμ // hh:mm (Greece)

I used el-GR as that was the culture that the user that reported the problem with, he also tested this on Vista SP2 and Win 7 RC with the same result.

The question is two-fold really:
1) What is my misunderstanding of .NET and Windows Formats?
2) What is the best solution to create a short format time string (HH:mm or hh:mm tt) based upon the operating system, ideally this should work in Mono so I would prefer to avoid reading from the registry or P/Invoke.

Method used to generate the above, for future reference and testing.

[STAThread]
static void Main(string[] args)
{
    CultureInfo culture = CultureInfo.CurrentCulture;

    DateTime sixAm = new DateTime(2009, 07, 05, 6, 0, 0); // 6AM 
    DateTime sixPm = new DateTime(2009, 07, 05, 18, 0, 0); // 6PM

    string sixAmString = sixAm.ToShortTimeString();
    string sixPmString = sixPm.ToShortTimeString();

    string format = "Culture: {0}, 6AM: {1}, 6PM: {2}";

    string output = String.Format(format, culture, sixAmString, sixPmString);
    Console.WriteLine(output);
    Clipboard.Clear();
    Clipboard.SetText(output);

    Console.ReadKey();
}

Update:
Based upon Mike’s comments below I adapted the above method with the following changes:

The following two lines

string sixAmString = sixAm.ToShortTimeString();
string sixPmString = sixPm.ToShortTimeString();

Changed to

string sixAmString = sixAm.ToString("t", culture);
string sixPmString = sixPm.ToString("t", culture);

I also changed the culture variable to use CultureInfo.CurrentUICulture.

This unfortunatly didn’t work as well as I had hoped, the output regardless of the configuration of Short Time in Windows 7’s Formats tab was:

Culture: en-US, 6AM: 6:00 AM, 6PM: 6:00 PM

It seems the CultureInfo.CurrentUICulture is always en-US.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:11:17+00:00Added an answer on May 12, 2026 at 9:11 am

    In answer to each of my questions:

    1) What is my misunderstanding of .NET and Windows Formats?

    The short answer is, there is no link between the “Short Time” setting in “Regional and Language” settings and .NET’s ShortTimePattern property. However the LongTimePattern property is dictated by the “Long Time” setting.

    I adapted the above method replacing the two formatting lines to:

    string sixAmString = sixAm.ToString("T", culture.DateTimeFormat);
    string sixPmString = sixPm.ToString("T", culture.DateTimeFormat);
    

    Here is the output:

    Culture: en-GB, 6AM: 06:00:00, 6PM: 18:00:00 // HH:mm:ss
    Culture: en-GB, 6AM: 06:00:00 AM, 6PM: 06:00:00 PM //hh:mm:ss tt
    

    The bottom of this article explained the problem to me.

    2) What is the best solution to create a short format time string (HH:mm or hh:mm tt) based upon the operating system setting?

    I don’t know about the best solution, but I created the following function that converts the LongTimeFormat to a ShortTimeFormat thus allowing an application to follow the users option if they change the “Long Time” (albeit it won’t track the “Short Time” setting).

    static string GetShortTimeString(DateTime ShortTimeString)
    {
        DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
        string ShortTimePattern = dateTimeFormat.LongTimePattern.Replace(":ss", String.Empty);
        ShortTimePattern = ShortTimePattern.Replace(":s", String.Empty);
    
        return ShortTimeString.ToString(ShortTimePattern);
    }
    

    The output after making the above changes:

    Culture: en-GB, 6AM: 06:00, 6PM: 18:00
    Culture: en-GB, 6AM: 06:00 AM, 6PM: 06:00 PM
    

    The P/Invoke option is to use GetTimeFormat passing the TIME_NOSECONDS using DateTime.ToString(Format) as above. I have not tested this as I would prefer to avoid using P/Invoke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying out Mongoid at the moment, I've run into an issue that's probably
I have run into an issue where an input field within a form I'm
I am creating a Node.js app on Heroku and have run into an issue.
I am creating my first (!) database, and I have run into an issue
I have run into a small issue in my program. I have a class
I have run into a bit of a design issue with my code. I
Trying to solve an issue for someone else and instead have run into my
I have recently run into a particularly sticky issue regarding committing the result of
I have run into a little problem. I am connecting to a webservice that
I'm building a web-application with MS MVC 3 and have run into an issue,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.