[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 717
  • Last Modified:

Windows Taskbar Time Format

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.

regards,
Ferry

0
huferry
Asked:
huferry
  • 8
  • 3
  • 3
  • +2
1 Solution
 
TNameCommented:
How are you trying to retrieve it?

A simple
ShowMessage(TimeToStr(Now));
works for me, as it displays AM/PM or not, depending on the settings.
(Reg setting English(US)-just for this test ;)  D7, XPSP2)
0
 
huferryAuthor Commented:
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.
0
 
TNameCommented:
>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"
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
huferryAuthor Commented:
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.
0
 
TNameCommented:
>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.
0
 
huferryAuthor Commented:
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.

regards,
Ferry
0
 
cobi100Commented:
have you tried with FormatDateTime from the unit DateUtils?

like:
  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
0
 
saravananvgCommented:
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.

ShowMessage(Copy(FormatDateTime('t',Now),1,Length(FormatDateTime('t',Now))2));

with regards,
padmaja.
0
 
huferryAuthor Commented:
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.
0
 
sun4sundayCommented:
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
  PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);

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

sun4sunday.
0
 
huferryAuthor Commented:
sun4sunday> sorry, i'm not asking to set the time format, but to retrieve the time format.
0
 
cobi100Commented:
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):

var
  aTimePrefix, aTimePostfix, aHourFormat, aShortTimeFormat : string;
begin
  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'
    else
      aTimePrefix := 'AMPM ';
  aShortTimeFormat := aTimePrefix + aHourFormat + ':mm' + aTimePostfix;
  ShowMessage(aShortTimeFormat);
  ShowMessage(FormatDateTime(aShortTimeFormat, now));
end;

0
 
huferryAuthor Commented:
cobi100,

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

0
 
cobi100Commented:
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:

var
  aShortTimeFormat : string;
begin
  aShortTimeFormat := StringReplace(GetLocaleStr(GetUserDefaultLCID , LOCALE_STIMEFORMAT, '0'), ':ss', '',[rfReplaceAll, rfIgnoreCase]);
  aShortTimeFormat := StringReplace(aShortTimeFormat, 'tt', 'AMPM',[rfReplaceAll, rfIgnoreCase]);
  ShowMessage(FormatDateTime(aShortTimeFormat, now));
end;
0
 
huferryAuthor Commented:
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'.

regards,
Ferry
0
 
huferryAuthor Commented:
Since noone can answer the question. I'd like to request a refund.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now