473,473 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DateTime.ToShortTimeString and 24 hour time

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
7 36517
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
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
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lauren Wilson | last post by:
Is it possible to create a data input mask for a text box bound to a date/time field so that it will display and guide entry of the time values in 24 hour (military) time format?
2
by: sho_nuff | last post by:
Hello all, I have several DateTime objects and i want to get the average of all of them. What would be the easiest way to do this? Thanks SN
3
by: M.Smith | last post by:
I am returning a datetime from SQL and need to populate a field with just the time... I have tried: DateTime.Parse(TheTime.Hour.ToString()) + ":" + DateTime.Parse(TheTime.Minute.ToString();
1
by: Doug Swanson | last post by:
I have some questions about datetime formatting....In our web app I have the following in the web.config <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US"...
1
by: Roy | last post by:
I have an ASP page that I an using to read from one data base and load into another. This ASP takes more than an hour to execute. After abount an hour to an hour and a half I get the error "Cannot...
3
by: Andrew Chalk | last post by:
I have the time of an event stored on each record as a datetime field.It includes the year,month,day, etc. Suppose my user wants to search the table for all events over the lunch hour, say...
6
by: Dogmar Hoffman | last post by:
Hello, I am trying to use tostring to convert to a 24 hour time format, but am getting the error "No Overload for Method 'ToString'takes '1' arguments". The following is what I have and it...
1
by: Mike the Canadian | last post by:
Suppose I have a MySQL 5 table with a DateTime field in it. The table has two records where the time is blank. Running: SELECT CAST(DateTimeField AS TIME) FROM MyTable shows two records with...
1
by: kalisha | last post by:
I uploaded data in to postgresql using pdadminIII ,In one the fields called 'time' i want to store time as 24 hour format .Is there a way where i can convert time in to 24 hour format and store in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.