Lazarus Comparing Dates

Asked

Viewed 162 times

0

In the data collector I can configure the format of Data, but I want to make this configuration within my application, I am trying to do as follows:

FormatSettings.ShortDateFormat := 'dd-mm-yy';
ShowMessage(QueryTesteDT_PALLET.Text); // Resultado: '06-07-17'
if QueryTesteDT_PALLET.Value <= StrToDate('30-06-17') then
....

I have the following error: "30-06-17" is not a Valid date format.

I’m using Lazarus for development.

2 answers

0

You must pass the Formatsettings you have set as parameter of the Strtodate function. You also need to assign the separator you want to use to the attribute Formatsettings.Dateseparator.

Strtodate function has overload, so you can use it in two ways:

The first form takes only the string and converts to date with the standard Shortdateformat format. Function Strtodate(const S: string): Tdatetime; Overload;

The second way to use the function, gets the string to be converted and the Formatsettings to be used. Function Strtodate(const S: string; const Formatsettings: Tformatsettings): Tdatetime; Overload;

Follow Example of use:

procedure TForm1.Button1Click(Sender: TObject);
var
  Data: TDateTime;
  FormatSettings: TFormatSettings;
  begin
    FormatSettings.DateSeparator := '-';
    FormatSettings.ShortDateFormat := 'dd/mm/yyyy';
    Data := StrToDate('01-06-2017', FormatSettings);
    ShowMessage(DateToStr(Data));
end;

0

I’ve never used Lazarus, but...

if Querytestedt_pallet.Value <= Strtodate('30-06-17') then

I used to do something like this:

if Querytestedt_pallet.Asdate <= 30-06-17 then // or 30.06.17 or 30/06/17

If you don’t have "asDate" or asDateTime typing. It can be interesting to pass everything to String and use one btween.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.