How to compare dates with today's date?

Aravindh_NavisionAravindh_Navision Member Posts: 258
edited 2014-09-26 in NAV Tips & Tricks
Hi Friends,

In a report I have declared 4 date variables dtA, dtB, dtC, dtD and assigning dates to these variables from 4 different tables.

Case 1:
For example:
dtA = 25/08/2014 (from Vendor Ledger Entry)
dtB = 01/09/2014 (from Purchase Header)
dtC = 29/08/2014 (from Change Log Entry)
dtD = 26/08/2014 (from Vendor - Last Modified Date field)

I need to compare each of the above dates with today's date and take the date which is nearer to today's date. Say in this case 01/09/2014 (dtB) is the one.

Case 2:
I need to add these dates in a temporary table for any date field and sort it in incremental order and I can take the last value. This is also an option. How can I add the above dates in a temporary table?

Can anyone explain me how to do these 2 cases? Also tell me which one is the easiest one.

Thanks in advance.

Comments

  • MBergerMBerger Member Posts: 413
    function GetClosestDate ( Date1 : Date, Date2 : Date ) : Date
    Begin
      if date1 = 0d then exit(date2) ;
      if date2 = 0d then exit(date1) ;
    
      if abs(today - date1) < abs(today - date2) then
        exit(date1)
      else
        exit(date2) ;
    end ;
    
    then in your main code :
    Closestdate := dtA ;
    Closestdate := GetClosestdate(Closestdate,dtB) ;
    Closestdate := GetClosestdate(Closestdate,dtC) ;
    Closestdate := GetClosestdate(Closestdate,dtD) ;
    

    Your approach using a temp. table won't work as you state it, because the highest date is not necessarily the one closest to today.
  • Aravindh_NavisionAravindh_Navision Member Posts: 258
    Thank you Berger. It worked... :thumbsup:
Sign In or Register to comment.