Hi All

how can I change System time format From hh:mm:ss(12 Hour) to HH:mm:ss(24 Hour) Using Delphi code?
Thanks

Recommended Answers

All 10 Replies

Are you talking about Delphi's ShortDateFormat /ShortTimeFormat /LongDateFormat /LongTimeFormat variables (in SysUtils)?

Or are you talking about changing something in the OS?

Dear Duas
Thankyou for Your Answer.
about your question I mast Say No I want to changing format of OS time by sume Delphi instructions

Dear Duas
Thankyou for Your Answer.
about your question I mast Say No I want to changing format of OS time by sume Delphi instructions

One way would be to change the Windows Registry settings directly. The entries you are looking for are the sLongDate, sShortDate and sTimeFormat strings under HKEY_CURRENT_USER\Control Panel\International. You can use the Delphi TRegistry object to manipulate these entries.

Thankyou very much "ExplainThat "
whold you please send me a simple Example code for using registry Object?
Thankyou

Thankyou very much "ExplainThat "
whold you please send me a simple Example code for using registry Object?
Thankyou

End of my working day here so a detailed example will have to wait for tomorrow. If you really want to have a go at it right away just take a look at the TRegistry object methods in Delphi help. Your code would go something like this

with TRegistry.Create(Self) do
try
  OpenKey(...);
  //modify key contents here
finally Free end;

Quoting from memory so there might be errors. TRegistry is not hard to play with so you might find that the examples in Delphi Help sufficient. Do post a message saying if you have managed on your own so I don't end up spending time telling what you already know.

Thankyou "ExplainThat "
I write Same code And I can Change Registry
But I mast restart computer after runing code for making effect on time Format
is any way that dont need to restart system
Thanks

I've never done it, but I think you just need to tell the tray to repaint itself.
You should be able to do it with something like the following:

var
  shelltraywnd, traynotifywnd, clockwnd: HWND;
begin
  shelltraywnd := FindWindowEx( 0, 0, 'shell_traywnd', nil );
  traynotifywnd := FindWindowEx( shelltraywnd, 0, 'traynotifywnd', nil );
  clockwnd := FindWindowEx( traynotifywnd, 0, 'trayclockwclass', nil );
  SendMessage( clockwnd, WM_PAINT, 0, 0 )
end;

Let us know if that doesn't work.

I've never done it, but I think you just need to tell the tray to repaint itself.
You should be able to do it with something like the following:

var
  shelltraywnd, traynotifywnd, clockwnd: HWND;
begin
  shelltraywnd := FindWindowEx( 0, 0, 'shell_traywnd', nil );
  traynotifywnd := FindWindowEx( shelltraywnd, 0, 'traynotifywnd', nil );
  clockwnd := FindWindowEx( traynotifywnd, 0, 'trayclockwclass', nil );
  SendMessage( clockwnd, WM_PAINT, 0, 0 )
end;

Let us know if that doesn't work.

I am almost certain that works. In case it does not, here is a trick I remember using

  • Change the registry strings
  • Use the Windows GetLocalTime function to fetch the current time in a SystemTime structure.
  • Sleep(1)
  • Call Windows SetLocalTime using the SystemTime data obtained above.

I remember doing this but not with WindowsXP. Seem to recall that I did a Sleep(1) to ensure that Windows recognized that the time was in fact different.

This is nothing short of a cheap hack. Try the tray repaint technique first.

Ah, glad I wasn't completely off in left field. I was sure ExplainThat had a better clue here than I...

The sleep() is required to give the registry time to update (it isn't absolutely instantaneous).

Member Avatar for Micheus

I write Same code And I can Change Registry
But I mast restart computer after runing code for making effect on time Format
is any way that dont need to restart system

I'm have answered this question for You last July. Doesn't You remember? (post)

It was a procedure called SetTimeFormat with a litle compiler error (it's true): the parameter was bad named.
But, you don't reply or report me this mistake, and I don't have perceived: I had put tfType24 but must be ftType24.

procedure SetTimeFormat(ftType24 :Boolean)
var
  Changed :Boolean;
  LCID :LongInt;
begin
  LCID := GetSystemDefaultLCID;
  if ftType24 then  // if format type is 24 hours...
    Changed := SetLocaleInfo(LCID, LOCALE_STIMEFORMAT, 'HH:mm:ss')
  else  // if format type is 12 hours AM/PM
    Changed := SetLocaleInfo(LCID, LOCALE_STIMEFORMAT, 'hh:mm:ss');

   if Changed then  // if no error then notify all app that settings changed
     SendMessage(HWND_TOPMOST, WM_SETTINGCHANGE, 0, 0);
 end;

This procedure work right to me.
No access to register and no computer restart are necessary.

Bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.