Solved

Windows Taskbar Time Format

Posted on 2006-10-24
16
703 Views
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.

regards,
Ferry

0
Comment
Question by:huferry
  • 8
  • 3
  • 3
  • +2
16 Comments
 
LVL 28

Expert Comment

by:TName
ID: 17794191
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
 
LVL 3

Author Comment

by:huferry
ID: 17794207
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
 
LVL 28

Expert Comment

by:TName
ID: 17794463
>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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Author Comment

by:huferry
ID: 17794757
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
 
LVL 28

Expert Comment

by:TName
ID: 17795287
>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
 
LVL 3

Author Comment

by:huferry
ID: 17795337
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
 
LVL 3

Expert Comment

by:cobi100
ID: 17796523
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
 
LVL 6

Expert Comment

by:saravananvg
ID: 17801650
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
 
LVL 3

Author Comment

by:huferry
ID: 17801715
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
 
LVL 9

Expert Comment

by:sun4sunday
ID: 17801852
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
 
LVL 3

Author Comment

by:huferry
ID: 17802220
sun4sunday> sorry, i'm not asking to set the time format, but to retrieve the time format.
0
 
LVL 3

Expert Comment

by:cobi100
ID: 17803860
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
 
LVL 3

Author Comment

by:huferry
ID: 17809799
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
 
LVL 3

Accepted Solution

by:
cobi100 earned 250 total points
ID: 17812448
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
 
LVL 3

Author Comment

by:huferry
ID: 18291427
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
 
LVL 3

Author Comment

by:huferry
ID: 19800430
Since noone can answer the question. I'd like to request a refund.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question