Solved

Comparing dates in Delphi. Greater than/ Less than

Posted on 2016-07-27
3
155 Views
Last Modified: 2016-07-27
I have a TcxGrid and on a DB column on validate i run the following code.

  
sDate := VarToStr(DisplayValue);
  sNew  := (DateToStr(Now + 49));
  if (sDate < sNew) then begin 
    MessageDlg('Please enter a Committed Delivery Date that is 7 weeks from today.', mtError, [mbOK], 0);
    gvMasterChangeLogCommittedDelivery.DataBinding.Field.Clear;
    gvMasterChangeLog.DataController.DataSet.FieldValues['PlannedInstall']  := sInstall; //INSTALL
    gvMasterChangeLogCommittedDelivery.Focused;
    exit;
  end;

Open in new window


the problem is that if someone picks a date in the following month (October, greater than September) it still runs the error message.
0
Comment
Question by:Bianca
  • 2
3 Comments
 
LVL 14

Expert Comment

by:SteveBay
ID: 41731533
Why are you comparing them as strings?

It would be more accurate to compare two dates (which are double values) rather than dates that have been converted to strings.

'10' < '9' = True // String comparison
10 < 9 = False // Date (numeric) comparison.
0
 

Author Comment

by:Bianca
ID: 41731569
I'm not sure how to do that. How do i take the selected date and compare it to today + 49 (Seven weeks)?
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 41731631
Assuming that "DisplayValue" is a Variant it would look something like this:
procedure TForm1.Button1Click(Sender: TObject);
var  DisplayValue : Variant;
     dDate, dNew : TDate;
begin
     DisplayValue := DateTimePicker1.DateTime;
     dDate := VarToDateTime(DisplayValue);
     dNew := Now + 49;
     if dDate < dNew then
          ShowMessage('Date is too soon')
     else
          ShowMessage('Date is Acceptable');
end;

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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