Solved

Date problem. regional settings

Posted on 1998-09-21
12
763 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

15 Experts available now in Live!

Get 1:1 Help Now