By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

DateTime.ToShortTimeString and 24 hour time

P: n/a
Hello-

I live in the US, however I'm attempting to make a product of mine more
international-friendly. There are a number of instances where a calendar
function of my program displays various times, which is displayed using
DateTime.ToShortTimeString(). This always displays them in an AM/PM format.
When I go into the test computer's locality and change the time format to 24
hour time, the program still displays times in AM/PM format. Is there a
better way to implement this, so that either AM/PM or 24 hour time will be
displayed based upon the user's locality? Thank you.

Robert
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Robert,

Are you sure you are changing the time correctly. Remember, to get rid of the AM/PM symbol you need to remove the 'tt' part. Also, just changing the time does not appear to affect the program, you need to rebuild it (not just hit F5, which will do nothing if you haven't changed the code).

To test how it would appear it different localizations you can programmatically change the programs "location".

Thread.CurrentThread.CurrentCulture = new CultureInfo("nb-NO");
// Norwegian bokmål, uses a 24 hour clock
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

P: n/a
Hello Morten:

Thanks for your reply. I'm pretty sure that I changed the time format
correctly on the test system as changing it changed the time shown in the
system tray to 24 hour format. However, my goal is not to rebuild many
different versions of the program for use in different localities; I'd like
the program to be able to determine the local time format (as configured in
the Control Panel at least) and display it accordingly. Is this stored
somewhere in the registry?

Robert
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr76jv3olklbvpo@morten_x.edunord...
Hi Robert,

Are you sure you are changing the time correctly. Remember, to get rid of the AM/PM symbol you need to remove the 'tt' part. Also, just changing the
time does not appear to affect the program, you need to rebuild it (not just
hit F5, which will do nothing if you haven't changed the code).
To test how it would appear it different localizations you can programmatically change the programs "location".
Thread.CurrentThread.CurrentCulture = new CultureInfo("nb-NO");
// Norwegian bokmål, uses a 24 hour clock
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3

P: n/a
Hm, odd, DateTime.ToShortTimeString seems to ignore customized time formats alltogether. Rebuilding won't matter.
If you change your current country settings the program should detect it fine without the need for a rebuild.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

P: n/a
You're right. Initially I kept the locality as US/English and only changed
the time format (which should have worked anyway, but that's another story.)
I just now changed the locality to Norwegian (Bokmal) and it displayed the
times correctly, and even displayed the names of the months in Norwegian, as
I had hoped. Guess we already are international-friendly. :-) Thanks for
your help.

Robert

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr76mf9b0klbvpo@morten_x.edunord...
Hm, odd, DateTime.ToShortTimeString seems to ignore customized time formats alltogether. Rebuilding won't matter. If you change your current country settings the program should detect it fine without the need for a rebuild.
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #5

P: n/a
Hi Robert!
Hello-

I live in the US, however I'm attempting to make a product of mine more
international-friendly. There are a number of instances where a calendar
function of my program displays various times, which is displayed using
DateTime.ToShortTimeString(). This always displays them in an AM/PM format.
When I go into the test computer's locality and change the time format to 24
hour time, the program still displays times in AM/PM format. Is there a
better way to implement this, so that either AM/PM or 24 hour time will be
displayed based upon the user's locality? Thank you.

Robert


Use DataTime custom formatting!
There's no philosophy in time format, so i can recomend
to use this:

DateTime.ToString("HH:mm:ss");
// besides of DateTime.ToString("hh:mm:ss");

For more info, look in .NET docs for "DateTimeFormatInfo"

Cheers!

Marcin
Nov 16 '05 #6

P: n/a
> DateTime.ToString("HH:mm:ss");
// besides of DateTime.ToString("hh:mm:ss");


Would you know how to add the AM/PM part?
DateTime.ToString("hh:mm:ss tt") won't do anything since I have no defined AM or PM symbols in the regional settings.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7

P: n/a
Morten Wennevik wrote:
DateTime.ToString("HH:mm:ss");
// besides of DateTime.ToString("hh:mm:ss");

Would you know how to add the AM/PM part?
DateTime.ToString("hh:mm:ss tt") won't do anything since I have no
defined AM or PM symbols in the regional settings.
Happy coding!
Morten Wennevik [C# MVP]


You simply don't need to do this. Try the following code:

public static void Main()
{
string dateString = "Tue, 27 Apr 2004 08:16:13 GMT";
DateTime dateTime = DateTime.Parse(dateString);

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine(Thread.CurrentThread.CurrentCult ure.EnglishName);
Console.WriteLine("{0}", dateTime.ToString("T"));

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine(Thread.CurrentThread.CurrentCult ure.EnglishName);
Console.WriteLine("{0}", dateTime.ToString("T"));

Console.ReadLine();
}

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.