We help IT Professionals succeed at work.

Windows Taskbar Time Format

huferry asked
Medium Priority
Last Modified: 2008-02-20
How do retrieve the time with the exact format as it is shown in the windows taskbar?
The point is if I set the time format (in control panel) as: h:mm:ss (using the 12-hour notation AND without the AM/PM) getting the time format in Delphi, I still get the AM/PM.


Watch Question

Top Expert 2007

How are you trying to retrieve it?

A simple
works for me, as it displays AM/PM or not, depending on the settings.
(Reg setting English(US)-just for this test ;)  D7, XPSP2)


First of all, it shows the 'second' while it is not shown in the task bar.
Try this.. set the 12-hour format and without the am/pm in the control panel,
this function still shows the am/pm.
Top Expert 2007

>First of all, it shows the 'second' while it is not shown in the task bar.
As far as I know the seconds are never shown in the task bar (without the help of third party tools, that is...). That doesn't depend on the time format settings.

>Try this.. set the 12-hour format and without the am/pm in the control panel, this function still shows the am/pm.
No, as I said, for me it doesn't.
If AM/PM is selected in the regional settings, TimeToStr(Now) will result in "12:30:00 PM"
If AM/PM is deselected, TimeToStr(Now) will return "12:30:00"


strange enough that TimeToStr will display the am/pm when I use the format 'h:mm:ss' in regional settings.
Note that the small 'h' denotes the 12-hour format and the capital 'H' the 24-hour format.
Top Expert 2007

>strange enough that TimeToStr will display the am/pm when I use the format 'h:mm:ss' in regional settings.

No, I think it would be strange if it displayed am/pm when using 'H:mm:ss'. Then you'd get 21:00:00 PM, which wouldn't make any sense.
Your problem is apparently that you get am/pm displayed even if you leave the am/pm boxes empty.

Ok, now it's something like 14:50 CET
If I select 'h:mm:ss'  and NO am/pm (boxes are left blank) then I get 2:50:00
If I select 'h:mm:ss'  WITH am/pm  then I get 2:50:00 PM
If I select 'H:mm:ss'  then I get 14:50:00 regardless if the am/pm boxes are empty or not.


The point is:
1. regardless if it makes sense, I would like to have the same format as it is in the task bar.
   It's just the requirement that I got for this project. If it looks strange, then it's not my
   fault, it's the user's that has set the regional settings.
2. Your second case 'h:mm:ss' WITH am/pm does not meet my requirement since you'd have
    to define 'tt' to get the am/pm on your string.


have you tried with FormatDateTime from the unit DateUtils?

  ShowMessage(FormatDateTime(ShortTimeFormat, now));

instead of ShortTimeFormat you could pass 'h:mm'

or check if AM PM is included in ShortTimeFormat, with the global TimeAMString

you can find more info on the help topic "Currency and date/time formatting variables" in delphi
Hello Sir.

  You do not have functions to just display only the time without AM or PM with shorttime formats or any format, only thing you need to do is to manipulate the returnvalue to display the desired.


with regards,


cobi100: sure I know that function, the point is you don't know whether the user settings in control panel is 'h:mm'.
saravnanvg: FormatDateTime, won't work form my problem.
If you want to control the date format

//  Place the code on the starting of the project

  uses Windows, Messages, SysUtils,

  DateSeparator := '/';
  ShortDateFormat := 'dd/MM/yyyy';
  SetLocaleInfo(GetThreadLocale, LOCALE_SSHORTDATE, PChar('dd/MM/yyyy')); //set locale to right format

Even you can store the existing datetime format and put it back when you exit the application



sun4sunday> sorry, i'm not asking to set the time format, but to retrieve the time format.

huferry, the ShortTimeFormat is retrieved from the system, from the help:

ShortTimeFormat      ShortTimeFormat is the format string used to convert a time value to a short string with only hours and minutes. The default value is computed from LOCALE_ITIME and LOCALE_ITLZERO.

these values are retrieved using the GetLocaleInfo function in the Win32 API, inspecting the sysutls code it seems like GetLocaleInfo is called with the GetThreadLocale as the LCID param, I think this will reflect the current user's settings, but it seems in your case it is not, I recomment making a call to GetLocaleInfo with GetUserDefaultLCID instead, something like this (partially taken from sysUtils):

  aTimePrefix, aTimePostfix, aHourFormat, aShortTimeFormat : string;
  aTimePrefix := '';
  aTimePostfix := '';
  if StrToIntDef(GetLocaleStr(GetUserDefaultLCID, LOCALE_ITLZERO, '0'), 0) = 0 then
    aHourFormat := 'h' else
    aHourFormat := 'hh';
  if StrToIntDef(GetLocaleStr(GetUserDefaultLCID, LOCALE_ITIME, '0'), 0) = 0 then
    if StrToIntDef(GetLocaleStr(GetUserDefaultLCID, LOCALE_ITIMEMARKPOSN, '0'), 0) = 0 then
      aTimePostfix := ' AMPM'
      aTimePrefix := 'AMPM ';
  aShortTimeFormat := aTimePrefix + aHourFormat + ':mm' + aTimePostfix;
  ShowMessage(FormatDateTime(aShortTimeFormat, now));



My Time format on Regional Settings is 'h:mm:ss' and my taskbar shows '8:51'
while your procedure showed me '8:51 am'.

ok, well that is really odd, it works like a charm in my system, you could try switching the GetUserDefaultLCID for GetSystemDefaultLCID or GetThreadLocale,

what I'm thinking is that you have a system locale different than your user locale, and on top of that you have customized the regional settings, or something along those lines,

so let's try something different, how about we get the time format straight from the API and remove the seconds, like this:

  aShortTimeFormat : string;
  aShortTimeFormat := StringReplace(GetLocaleStr(GetUserDefaultLCID , LOCALE_STIMEFORMAT, '0'), ':ss', '',[rfReplaceAll, rfIgnoreCase]);
  aShortTimeFormat := StringReplace(aShortTimeFormat, 'tt', 'AMPM',[rfReplaceAll, rfIgnoreCase]);
  ShowMessage(FormatDateTime(aShortTimeFormat, now));

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


I've tried the code, my regional options for time is: H:mm:ss tt, AM symbol=am, PM symbol=pm.

The time on my system tray showed '13:51 pm' while your code will return '1:51 pm'.



Since noone can answer the question. I'd like to request a refund.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.