Page 1 of 1

Days Caluclation between two dates

PostPosted: Fri Jun 22, 2012 7:16 pm
by rummylion
Hi,

I am beginner to Natural language and I have a requirement in which I need to calculate days between two given dates.
Does Natural language has any inbuilt functions to do the same??? If not, Can any one please help me out the best possible way to achieve it.


Regards,
Rummylion.

Re: Days Caluclation between two dates

PostPosted: Fri Jun 22, 2012 7:22 pm
by Akatsukami
Can the LE date manipulation functions be called from Natural?

Re: Days Caluclation between two dates

PostPosted: Fri Jun 22, 2012 7:36 pm
by rummylion
No, I am supposed to write this using Natural language only.

Re: Days Caluclation between two dates

PostPosted: Sat Jun 23, 2012 12:06 am
by dick scherrer
Hello and welcome to the forum,

Maybe Ralph Zbrog will see this and reply.

Here is something he posted in another forum:
. . . there is a common practice is to convert your external date variable (eg: yyyymmdd) to a temporary D-format variable using a MOVE EDITED. The D-format makes comparisons and computations simple and straightforward.


With this guidance and the documentation, hopefully you can proceed.

Re: Days Caluclation between two dates

PostPosted: Sat Jun 23, 2012 2:47 am
by prino
/**********************************************************************
* Convert a date to a Julian Day Number                               *
**********************************************************************/
cj: proc(fld, flm, fly, jdn);
dcl fld float     (16);
dcl flm float     (16);
dcl fly float     (16);
dcl jdn fixed bin (31);

  fly = fly + (flm - 2.85) / 12;

  jdn = floor(
          floor(
            floor(367 * fly) - 1.75 * floor(fly) + fld) -
              0.75 * floor(fly / 100)) + 1721115;
end cj;

/**********************************************************************
* Convert a Julian Day Number to a date                               *
**********************************************************************/
jc: proc(jdn, dd, mm, yyyy);
dcl jdn  fixed      (7);
dcl dd   fixed      (3);
dcl mm   fixed      (3);
dcl yyyy fixed      (5);

dcl flc  float     (16);
dcl fln  float     (16);

  fln  = jdn - 1721119.2;
  flc  = trunc(fln / 36524.25);
  fln  = fln + flc - trunc(flc / 4);

  yyyy = trunc(fln / 365.25);
  fln  = fln - trunc(365.25 * yyyy) - 0.3;
  mm   = trunc(fln / 30.6);
  dd   = trunc(fln - 30.6 * mm + 1);

  if mm > 9 then
    do;
      mm   = mm   - 9;
      yyyy = yyyy + 1;
    end;
  else
    mm = mm + 3;
end jc;

Re: Days Caluclation between two dates

PostPosted: Sun Jun 24, 2012 12:52 pm
by RGZbrog
Yes, Natural can handle date calculations natively, using any combination of date literals, date-format variables, or system date variable.

DEFINE DATA LOCAL
1 #FROM (D)  INIT <D'06/15/2012'>
1 #TO (D)    INIT <D'06/18/2012'>
1 #DAYS (N3)
END-DEFINE
#DAYS := D'06/18/2012' - D'06/15/2012'
WRITE #DAYS 'days between D"06/18/2012" and D"06/15/2012"'
*
#DAYS := #TO - #FROM
WRITE #DAYS 'days between #FROM and #TO'
*
#DAYS := *DATX - D'06/15/2012'
WRITE #DAYS 'days between *DATX and D"06/15/2012"'
END

Page     1                                                   06/24/12  00:19:56
 
   3 days between D'06/18/2012' and D'06/15/2012'
   3 days between #FROM and #TO
   9 days between *DATX and D'06/15/2012''

Alpha format date values must be converted to internal date format prior to the calculations.

Note that my date literals may not compile on your system, as the date format will be customized for your location. My Natural system expects date literals in the form D'MM/DD/YYYY'.

Re: Days Caluclation between two dates

PostPosted: Mon Jun 25, 2012 6:09 pm
by rummylion
Thank you for your help.