Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date problem. regional settings

Posted on 1998-09-21
12
Medium Priority
?
796 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

705 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