?
Solved

Comparing dates in Delphi. Greater than/ Less than

Posted on 2016-07-27
3
Medium Priority
?
620 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
[X]
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
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 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