Solved

Date problem. regional settings

Posted on 1998-09-21
12
766 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

19 Experts available now in Live!

Get 1:1 Help Now