Solved

Date problem. regional settings

Posted on 1998-09-21
12
786 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

623 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