DateTime.ToShortTimeString and 24 hour time

R

Robert Misiak

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
 
M

Morten Wennevik

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]
 
R

Robert Misiak

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 said:
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]
 
M

Morten Wennevik

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]
 
R

Robert Misiak

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 said:
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]
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

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
 
M

Morten Wennevik

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]
 
E

Ed Courtenay

Morten said:
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.CurrentCulture.EnglishName);
Console.WriteLine("{0}", dateTime.ToString("T"));

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

Console.ReadLine();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top