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
Solved

Comparing dates in Delphi. Greater than/ Less than

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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