Safely Enter Date String into TDateTimePicker

surfbored
surfbored used Ask the Experts™
on
I want to convert a string that looks like this "2009-12-31" into a TDate, SAFELY.

I'm worried that my TDateTimePicker will not load the date correctly on machines with different regional settings (possibly confusing the day and the month).

Can anyone with experience offer advice/code?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Geert GOracle dba
Top Expert 2009

Commented:
in your program you can set the regional settings for your program

at startup you can set any of the global delphi variables

Application.UpdateFormatSettings := False;
will make sure they stay like that when somebody changes windows regional settings while your program is running
procedure TMainForm.FormCreate(Sender: TObject);
begin
  DecimalSeparator := '.';
  ShortDateFormat := 'YYYY-MM-DD';
  Application.UpdateFormatSettings := False;
end;

Open in new window

Author

Commented:
Geert:

Thanks for your suggestion, but I would still like the user to see the date in the format they expect to see it, otherwise they're likely to enter the wrong value.

I know I can convert the date properly from the DateTimePicker (in fact I do that already), I just don't know how to put it back into the DateTimePicker correctly. Any other suggestions?

 
Geert GOracle dba
Top Expert 2009

Commented:
function YourStrToDate(aString: string): TDateTime;
var sdf: string;
begin
  sdf := ShortDateFormat;
  try
    ShortDateFormat := 'YYYY-MM-DD';
    Result := StrToDate(aString);
  finally
    ShortDateFormat := sdf;
  end;
end;
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Geert GOracle dba
Top Expert 2009

Commented:
the use that result to set the Data in the DateTimePicker

dtp.Date := YourStrToDate('2009-12-31');

Author

Commented:
Geert:

This is almost exactly the code I tried myself, but both yours and mine return the same exception:

EConvertError ... ''2008-12-31' is not a valid date'.

:(
Well you could you use
TryStrToDate or TryStrToDateTime

From Delphi Help File
Call TryStrToDateTime to parse a string that specifies a date and time value. If S does not contain a valid date, TryStrToDateTime returns false.

The above applies to TryStrToDate
function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean; overload;
function TryStrToDate(const S: string; out Value: TDateTime): Boolean; overload;

Open in new window

Author

Commented:
gtrifidis:

Thanks for the heads up on the functions, they should prove useful, but I'm not sure how they solves the current problem. If you have a clever use for these, I'm all ears! :)


Oracle dba
Top Expert 2009
Commented:
i tested it for myself and was amazed that this indeed does not work
but ... following does
you need to set the DateSeparator too
function YourStrToDate(aStr: string): TDateTime;
var fs: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, fs);
  fs.ShortDateFormat := 'YYYY-MM-DD';
  fs.DateSeparator := '-';
  Result := SysUtils.StrToDateTime(aStr, fs);
end;

Open in new window

Author

Commented:
Thanks for hanging in there. This answer was a great help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial