Solved

Comparing dates in Delphi. Greater than/ Less than

Posted on 2016-07-27
3
116 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtualstringtree carsh after invalidate for while 5 86
Printing problem 2 80
Multiple image collision 13 69
Multi-layered image in FireMonkey 9 16
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

24 Experts available now in Live!

Get 1:1 Help Now