Solved

Changing the system settings...

Posted on 1998-12-15
18
235 Views
Last Modified: 2012-05-04
Hello Experts...
How can I GET and SET the system Date formating string using Windows API ... like "dd/MMMM/yy' to "dd/MM/yy'

Thanx in advance
0
Comment
Question by:dyancer
  • 11
  • 6
18 Comments
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351057
hi, dyancer.

this is fragment gets the local date with the current settings:

function GetLocalDate: string;
var
  Buffer: array[Byte] of Char;
  iBufSize: Integer;
begin
  iBufSize := GetDateFormat(GetThreadLocale, LOCALE_NOUSEROVERRIDE, nil, nil, Buffer, SizeOf(Buffer));
  if iBufSize = 0 then
    Result := IntToStr(GetLastError)
  else
    Result := Buffer;
end;

the second parameter is dwFlags and can be e.g.
DATE_SHORTDATE or
DATE_LONGDATE  or something like that.
you can obtain this information from win32.hlp: "GetDateFormat".

i'm afraid this doesn't answer your question.
but maybe it's a hint that helps you a little bit.

have a nice day,

Black Death.
0
 
LVL 1

Author Comment

by:dyancer
ID: 1351058
Thank you BlackDeath for your try...
indeed before i post this question to the EX-EX i look in the win32 help and in the sysutils.pas and did not find any information on setting the system date format, but still hope there is someway to do this ...

Hassan
PS ."i've seen the code you posted at the sysutils.pas of delphi3"

0
 

Expert Comment

by:magno
ID: 1351059
Hi Dyancer,

First, I think that I did not got the problem in deep (also sorry my English, I am a brazilian), so I will repeat your question on my way to see if I can understand...Sorry if my answer doesnt match with your question:

"I need to change the date formatting string from DD/MM/YY to DD/MM/YYYY, how?"

Your actual question says about convert to "DD/MMMM/YY", but I guess it is mispelling, ok ? well, the SysUtils have some variables that allow that, let's see by a example:

----------8<---------------

 { Set this property to False to prevent the system settings from changing (registry) }
  Application.UpdateFormatSettings := FALSE;

  DateOrder := doDMY;     // or doMDY, p.ex  :)
  DateSeparator := '-';        // you could use a "." or "/" or any other char
  DateFullYear := True;    
  DateLeadZero := True;
  ShortDateFormat := 'dd/MM/yyyy';  // or 'dd/MM/yy' or 'd/m/yy' or 'd/mm/yyyy' etc...

  Memo1.Lines.Add('The OLD Date Formatting string is: '+ShortDateFormat);
  Memo1.Lines.Add(DateToStr(NOW)); // will print something like 25/12/1998
 
  ShortDateFormat := 'dd/MM/yy' ;

  Memo1.Lines.Add('The NEW Date Formatting string is: '+ShortDateFormat);
  Memo1.Lines.Add(DateToStr(NOW)); // will print something like 25/12/98

---8<----------

Is it what you want ? This questions can be solved by reading the "Currency and date/time formatting variables" in D4 help (D1 too, and I guess all delphi versions).

I really hope it help you, if not, tell me ok ?

Good Lucky !

PS: here is a function to set the system Date and Time, if you dont have one, I could be useful in future:

function SetNewTime(YY,MM, DD, hh , min : word): boolean;
var
  st:TSYSTEMTIME;
begin
     GetLocalTime(st);
     st.wYear := YY;
     st.wMonth := MM;
     st.wDay := DD;
     st.wHour := hh;
     st.wMinute := min;
     Result := SetLocalTime(st) ;
end;

0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351060
hi, magno.
i don't think it was a misspelling.

win32.hlp: Day, Month, Year, and Era Format Pictures
--
The format picture for a date string consists of a combination of null-terminated strings.

Day      
d      Day of the month as digits without leading zeros for single digit days.
dd      Day of the month as digits with leading zeros for single digit days
ddd      Day of the week as a 3-letter abbreviation as given by a LOCALE_SABBREVDAYNAME value.
dddd      Day of the week as given by a LOCALE_SDAYNAME value.
Month      
M      Month as digits without leading zeros for single digit months.
MM      Month as digits with leading zeros for single digit months
MMM      Month as a three letter abbreviation as given by a LOCALE_SABBREVMONTHNAME value.
MMMM      Month as given by a LOCALE_SMONTHNAME value.
Year      
y      Year represented only be the last digit.
yy      Year represented only be the last two digits.
yyyy      Year represented by the full 4 digits.

0
 
LVL 1

Author Comment

by:dyancer
ID: 1351061
Dear mango...
thanx alot for trying...
indeed your answer gave me no new information about setting the system date format string.
any way, in your answer there was a mistake...
you wrote " { Set this property to False to prevent the system settings from changing (registry) }
      Application.UpdateFormatSettings := FALSE;
"
this thing need some clarification:
when your program first load (if you use the sysutils unit) it read the system settings and save it into global variables, and for further use it does not read the settings again but use these global variables}
the  Application.UpdateFormatSettings := FALSE;  prevent the program from responding to WM_WININICHANGED (im not sure about the spelling) and by that prevent the variables in the sysutils from changing...

to say this will not help me set the system settings :(

my problem is the "SET" half not the "GET" half of the system date string format settings.

thanx.
dyancer
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351062
dyancer:
one more question:

shall this affect the whole system or just your application?

Black Death.
0
 
LVL 1

Author Comment

by:dyancer
ID: 1351063
Dear BlackDeath

I need to change the SYSTEM settings...
not the local copy of the system settings founds at sysutils unit.

thanx.
Dyancer
0
 
LVL 1

Author Comment

by:dyancer
ID: 1351064
I'll make it worth a try for you.... adjusting points to 300 ... :)

dyancer
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351065
well.
here's something:

HKEY_USERS\xxx\Control Panel\International
contains:

DefaultDialMode=P    (e.g.)
Locale=00000407    (germany)

and nothing else in default mode

but if you change the date format settings via
control panel - country settings (intl.cpl)
a new entry occurs:

sLongDate=d. MMM yyyy

so.
obviously this is user-specific.
i think you could set the systemwide date/time-format settings by changing this entry as desired and send a wininichanged (or whatever this one's called, i can't remember right now) through your system.

o.k., this ain't api, but up to now i couldn't find something else...
does this help?

rex,

Black Death.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351066
btw: you can use HKEY_CURRENT_USER\Control Panel\International as well.

Black Death.
0
 
LVL 1

Accepted Solution

by:
BlackDeath earned 300 total points
ID: 1351067
ah:

one step further.
check out SetLocaleInfo from win32.hlp.

.SetLocaleInfo only affects the user ovverride portion of the locale settings...

takes three parameters:

1st parameter would be: LOCALE_USER_DEFAULT
2nd parameter would be: LOCALE_SLONGDATE
3rd parameter would be: a pointer to a null-terminated string (lpstr (lpwstr for unicode) ) containing the format (e.g. "d. MMM. yyyy")

i didn't try it yet, but this should be what you've been looking for.

'til later,

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351068
see also: GetLocaleInfo
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351069
finally, i've got it:

open a new project.
place 3 tbuttons and two tedits on the form.
button1.caption := 'get'
button2.caption := 'set'
button3.caption := 'exit';

here's the source:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  p: PChar;
begin
  p := StrAlloc(255);
  if GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, p, 255) > 0 then
    Edit1.Text := StrPas(p)
  else
    case GetLastError of
      ERROR_INSUFFICIENT_BUFFER: Edit1.Text := 'insufficient buffer';
      ERROR_INVALID_FLAGS:       Edit1.Text := 'invalid flags';
      ERROR_INVALID_PARAMETER:   Edit1.Text := 'invalid parameter';
    end;
  StrDispose(p);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  p: PChar;
begin
  p := StrAlloc(255);
  p := PChar(Edit2.Text);
  if SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, p) then
    MessageDlg('settings changed', mtInformation, [mbOk], 0)
  else
    case GetLastError of
      ERROR_INSUFFICIENT_BUFFER: MessageDlg('insufficient buffer', mtError, [mbOk], 0);
      ERROR_INVALID_FLAGS:       MessageDlg('invalid flags', mtError, [mbOk], 0);
      ERROR_INVALID_PARAMETER:   MessageDlg('invalid parameter', mtError, [mbOk], 0);
    end;
end;

end.

this works fine on my machine.
you can control the result in the systray-clock, the date is displayed in the format you've just defined.

cheers,

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351070
remark:
edit1 displays the current settings
use edit2 to enter the format string you want to assign to sLongDate.

bye,

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351071
****:

in button2click in the source above the statement
StrDispose(p);
is missing at the end of the procedure.

sorry.

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1351072
dyancer: still alive? ;) Black
0
 
LVL 1

Author Comment

by:dyancer
ID: 1351073
Hello there,
I still alive, though i did not work in friday nor in saturday...
Oh... it seems like you worked alot on this...
Give me some time to check this and i'll contact you latter to tell you if it worked for me as well..
thanx
Dyancer (hassan)

PS. i knew about the settings in the registry but it does not worked for me because i couldnot force the system regional setting dialog to update it's values through the use of the WM_WININICHANGED.

0
 
LVL 1

Author Comment

by:dyancer
ID: 1351074
it works fine for me.
Thanx again...

dyancer


0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now