Solved

Date problem. regional settings

Posted on 1998-09-21
12
776 Views
Last Modified: 2012-05-04
Hi,

I've stored a date as a string in a table (for example 19980921). I want to display this date in a maskeditbox. My question is how can i know if the date must be displayed as 21/09/1998 or as 09/21/1998. Also when the program doesn't find a date the current date must be displayed. I tried  maskedit1.text := datetostr(date); but it seems that on some PC's the result is 21/09/__98. The format of the date can be different depending on regional settings of the PC. How can i get the full date and how can i know the regional settings.

The solution must work for Delphi 1,2,3  

Tanx in advance
0
Comment
Question by:Didi
  • 7
  • 3
  • 2
12 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1340401
Hi,
Use global variable ShortDateFormat.
Set it to 'yyyymmdd' or 'yyyy-mm-dd'.
Then DateToStr will work properly.

Check "Currency and date/time formatting variables" in the help file for more info.

/// John


0
 

Author Comment

by:Didi
ID: 1340402
This was a stupid question. I just found it myself.

Tanks anyway John
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340403
No problem.
And it wasn't a stupid question. No questions are stupid, only answers.

/// John
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 1

Expert Comment

by:BlackDeath
ID: 1340404
if you don't want the global variable ShortDateFormat to be affected, use

S := FormatDateTime('dd.mm.yyyy', Now); // or
DateTimeToString(S, 'dd.mm.yyyy', Now);


0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1340405
for locale date format see:

GetDateFormat

in win32.hlp.
if this isn't clear enough, tell me & i'll post an example.

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1340406
something else:

i think there's no possibility to distinguish between local datetime formats in 16bit-delphi (via winApi, that is - maybe there's a trick).

i came to this when browsing through the system libraries of delphi 32 and delphi 16.

in delphi 32 there's a call to GetDateFormat in DateTimeToString.
in delphi 16 there is nothing comparable.

here's an example of how to use GetDateFormat:

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;

cheers,

Black Death.

p.s.: if somebody finds a solution for 16bit, let us know...


0
 

Author Comment

by:Didi
ID: 1340407
Yes your right Black Death. I thought i had found the solution, but i was wrong. I still haven't a solution for the 16bit an  i really need a 16bit solution.

Tanks for your help.


0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1340408
i'm on the hunt...
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1340409
ok, here's something (not nice, but it should work):

when changing the country-settings, the formats for currency and datetime are written to win.ini, section [intl]. you can use TIniFile to read these settings and manually format your output.

this is the section for german settings:

[intl]
iCountry=49
ICurrDigits=2
iCurrency=3
iDate=1
iDigits=2
iLZero=1
iMeasure=0
iNegCurr=8
iTime=1
iTLZero=1
s1159=
s2359=
sCountry=Germany
sCurrency=DM
sDate=.
sDecimal=,
sLanguage=deu
sList=;
sLongDate=dddd, d. MMMM yyyy
sShortDate=dd.MM.yy
sThousand=.
sTime=:

and this is the section for american settings:

[intl]
iCountry=1
ICurrDigits=2
iCurrency=0
iDate=0
iDigits=2
iLZero=1
iMeasure=1
iNegCurr=0
iTime=0
iTLZero=0
s1159=AM
s2359=PM
sCountry=United States
sCurrency=$
sDate=/
sDecimal=.
sLanguage=enu
sList=,
sLongDate=dddd, MMMM dd, yyyy
sShortDate=M/d/yy
sThousand=,
sTime=:

so you can decide on:

case sDate of
  '/': {format date time the american way};
  '.': {format date time the german way};
  ...
else
  {format date time the default way (defined by you)}
end;

0
 
LVL 1

Accepted Solution

by:
BlackDeath earned 50 total points
ID: 1340410
take this:

function GetLocalDate16(dt: TDateTime): string;
var
  c: Char;
  p: PChar;
  s: string;
begin
  p := StrAlloc(Succ(MAX_PATH));
  GetWindowsDirectory(p, MAX_PATH);
  with TIniFile.Create(StrPas(p) + '\win.ini') do begin
    s := ReadString('intl', 'sDate', '.');
    c := s[1];
    case c of
      '/': GetLocalDate16 := FormatDateTime('mm/dd/yyyy', dt);
      '.': GetLocalDate16 := FormatDateTime('dd.mm.yyyy', dt);
    else
      Result := 'anything';
    end;
    Free;
  end;
  StrDispose(p);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := GetLocalDate16(Now);
end;

regards,
Black Death.

0
 

Author Comment

by:Didi
ID: 1340411
It isn't a very nice solution, but I think i can use it. if you post your comment  as an answer i will give you the points.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1340412
aehm, you can use as well :

p := StrAlloc(Succ(MAX_PATH));
GetWindowsDirectory(p, MAX_PATH);
with TIniFile.Create(StrPas(p) + '\win.ini') do begin         FormatDateTime(ReadString('intl', 'sDate', 'dd.mm.yyyy'), dt);
  Free;
end;
Dispose(p);

so you don't have to "case" this.
(obviously, win.ini is an ini-file, so why don't read the settings directly - sDate is a FormatString...)

cheers,

Black Death.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 390
How to debug For loops? 3 57
Tviruailstringtree sort multi columns on header click 1 76
Wincontrol not (correctly) drawn 15 42
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

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