Solved

Comparing dates in Delphi. Greater than/ Less than

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

17 Experts available now in Live!

Get 1:1 Help Now