Focal Point
[SOLVED]difference between 2 dates in WEEKS!

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/7837087606

July 12, 2011, 01:55 PM
Charles Richards
[SOLVED]difference between 2 dates in WEEKS!
How would you find the difference between 2 dates and put it in weeks, Both the dates are coming in as parameters!

Ex.

&SDATE MDYY ____3/1/2011
&EDATE MDYY ____3/15/2011

OUTPUT = I5_____2

This message has been edited. Last edited by: Charles Richards,


WebFOCUS 7.6
Windows, All Outputs
July 12, 2011, 02:09 PM
j.gross
In Dialog Manager, or in Reporting Language (Define)?

How do you want to handle cases where the difference is not an exact multiple of seven days?
July 12, 2011, 03:32 PM
Charles Richards
i would just like it to be rounded to the nearest integer, and a define would work!

ex
3/7 =0
4/7 =1


WebFOCUS 7.6
Windows, All Outputs
July 12, 2011, 03:52 PM
FrankDutch
subtracting dates can be done in many ways but as j.gross asked it depends if it are database fields or DM values.

please try to get the book "1001 ...." it helps a lot in date issues.

the rounding issue was posted by me with a function you can use.




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

July 12, 2011, 03:59 PM
Francis Mariani
I would use the DATEDIF function to determine the number of days between the two dates, then divide by 7 to determine the number of weeks.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
July 12, 2011, 04:10 PM
Dan Satchell
Or you could use legacy date function DAMDY. However, this approach assumes the input dates are always in the format 'mm/dd/yyyy', including leading zeroes.

-SET &SDATE = '03/05/2011';
-SET &EDATE = '03/15/2011';
-SET &SDATEX = EDIT(&SDATE,'99$99$9999');
-SET &EDATEX = EDIT(&EDATE,'99$99$9999');
-SET &WEEK_DIFF = (DAMDY(&EDATEX,'I8') - DAMDY(&SDATEX,'I8')) / 7 ;
-TYPE &WEEK_DIFF



WebFOCUS 7.7.05
July 18, 2011, 09:44 AM
Charles Richards
WKS/D12.2=DATEDIF(DAMDY(&SDATE, 'I8'), DAMDY(&EDATE, 'I8'), 'D');

I used the above line in my define and it doesn't seem to work, I can't seem to get this define working, is there an alturnative way, like calculating it with a compute?

INPUT

&SDATE
&EDATE


WebFOCUS 7.6
Windows, All Outputs
July 18, 2011, 09:55 AM
Francis Mariani
quote:
it doesn't seem to work, I can't seem to get this define working

When this occurs, it's helpful to tell us what error or incorrect result you got.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
July 18, 2011, 03:07 PM
Charles Richards
it calculates it to be either a 0 or .00, when i should be getting a 1.

&SDATE = 4/30/2011
&EDATE = 5/6/2011


WebFOCUS 7.6
Windows, All Outputs
July 18, 2011, 03:17 PM
Prarie
Isn't that because the difference between those dates is 6?
July 18, 2011, 03:25 PM
Dan Satchell
If you want a DEFINE/COMPUTE, try something like this:

-SET &SDATE = '4/30/2011';
-SET &EDATE = '5/6/2011';
-*
DEFINE FILE CAR
 SDATE/MDYY WITH COUNTRY = '&SDATE';
 EDATE/MDYY WITH COUNTRY = '&EDATE';
 WEEK_DIFF/F3 = (DATEDIF(SDATE,EDATE,'D')) / 7 ;
END
-*
TABLE FILE CAR
 PRINT SDATE EDATE WEEK_DIFF
 WHERE RECORDLIMIT EQ 1 ;
END

This message has been edited. Last edited by: Dan Satchell,


WebFOCUS 7.7.05
July 18, 2011, 03:33 PM
Charles Richards
SDATE/MDYY WITH COUNTRY = '&SDATE';



what does the "WITH COUNTRY" mean?

This message has been edited. Last edited by: Charles Richards,


WebFOCUS 7.6
Windows, All Outputs
July 18, 2011, 03:43 PM
Dan Satchell
Remove it and find out. In multi-segment files (like the CAR file) DEFINEs must reference a real field, a previously defined field, or must have an explicit field reference (e.g., 'WITH COUNTRY'). Otherwise FOCUS does not know to which segment it should attach the DEFINEd field.

This message has been edited. Last edited by: Dan Satchell,


WebFOCUS 7.7.05
July 19, 2011, 11:08 AM
Charles Richards
I can't figure out what i seem to be doing wrong...

I am running the fex file and added the define variable WKS int the header as
latest attempt:

SDATE/I8MDYY = DATECVT(&SDATE,'I8MDYY','MDYY');
EDATE/I8MDYY = DATECVT(&EDATE,'I8MDYY','MDYY');
WKS/D8 = (DATEDIF(SDATE,EDATE,'D')) / 7 ;

also tried

SDATE/I8MDYY = '&SDATE';
SDATE/I8MDYY = '&EDATE';
WKS/D8 = (DATEDIF(SDATE,EDATE,'D')) / 7 ;

HINT** the dates will be coming from a calender box from the HTML Composer

This message has been edited. Last edited by: Charles Richards,


WebFOCUS 7.6
Windows, All Outputs
July 19, 2011, 11:37 AM
Dan Satchell
SDATE/MDYY = '&SDATE';
EDATE/MDYY = '&EDATE';
WKS/D8 = (DATEDIF(SDATE,EDATE,'D')) / 7 ;



WebFOCUS 7.7.05
July 19, 2011, 01:28 PM
Charles Richards
Im still recieveing a 0 as the output, i even took the " / 7 " (divid by seven) out just to see if i get the difference in days and still a 0. I put the varible SDATE and EDATE in the header just to see what is getting put in them and the dates look exactly how they are suppose to, I don't know what I am doing wrong...

UPDATE: it seems to be working, but the output is 5,727

for
SDATE 04/30/2011
EDATE 05/06/2011

This message has been edited. Last edited by: Charles Richards,


WebFOCUS 7.6
Windows, All Outputs
July 19, 2011, 01:57 PM
Dan Satchell
I believe 5,727 is the number of weeks between 1/1/1901 and 5/6/2011. So it appears your code is using 1/1/1901 for SDATE instead of 4/30/2011. This would indicate an error somewhere in the code that handles the incoming value for &SDATE. The value for SDATE is therefore defaulting to the FOCUS base date of 1/1/1901. Put the following statement at the top of your program so you can see what is happening in your code.

-SET &ECHO = ALL ;



WebFOCUS 7.7.05
July 19, 2011, 03:17 PM
Charles Richards
ok I added that in, but it only works upon an error...

I added this to the header to see the values
"< WKS"
"< SDATE < EDATE"

and it is giving me

5,727
04/30/2011 05/06/2011

so It appears to be using the correct values


WebFOCUS 7.6
Windows, All Outputs
July 19, 2011, 03:59 PM
Francis Mariani
Works in this test fex:

-SET &SDATE = '04/30/2011';
-SET &EDATE = '05/06/2011';

DEFINE FILE CAR
SDATE/MDYY = '&SDATE';
EDATE/MDYY = '&EDATE';
WKS/D8 = (DATEDIF(SDATE,EDATE,'D')) / 7 ;
END
-RUN

TABLE FILE CAR
PRINT 
SDATE
EDATE
WKS
BY COUNTRY
WHERE RECORDLIMIT EQ 1
END
-RUN



Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
July 19, 2011, 04:35 PM
RSquared
Have you tried using the HDIFF function. It has a type of 'WEEK'?


WF 7.6.11
Oracle
WebSphere
Windows NT-5.2 x86 32bit
July 20, 2011, 09:16 AM
MLM
Hello charles you might want to check the date output from the date control, it gives you the option to automatically pass the date as yyyymmdd otherwise you have to convert the date from mm/dd/yyyy to yyyymmdd then use the datedif function.

-SET &ECHO = 'ALL';
-DEFAULT &SDATE = '20110228';
-DEFAULT &EDATE = '20110506';

TABLE FILE CAR
PRINT MODEL COUNTRY
COMPUTE DAYS/I8 = DATEDIF('&SDATE','&EDATE','D');
COMPUTE WKS/I8 = (DATEDIF('&SDATE','&EDATE','D'))/7;
END

try this


7610, Windows 64, Win 7
Excel, PDF, HTML
July 20, 2011, 10:16 AM
Charles Richards
Just tried your suggestion of HDIFF


Code:
WKS/I5 = (HDIFF('&SDATE','&EDATE','WEEK','I5'));

Echo:
WKS/I5 = (HDIFF('04/30/2011','05/06/2011','WEEK','I5'));


I can run francis's code and it works fine, then when i copy and paste it into my fex it doesn't work... i put it at the top of the fex file

might it have something to do with
SET SUMMARYLINES = NEW
thats the only code in front of it

i even hardcoded the dates in there and it still would not give me the correct values!

Code:


WebFOCUS 7.6
Windows, All Outputs
July 21, 2011, 10:11 AM
Charles Richards
quote:
-SET &SDATEX = EDIT(&SDATE,'99$99$9999');
-SET &EDATEX = EDIT(&EDATE,'99$99$9999');
-SET &WEEK_DIFF = (DAMDY(&EDATEX,'I8') - DAMDY(&SDATEX,'I8')) / 7 ;
-TYPE &WEEK_DIFF


ECHO:

-SET &SDATEX = EDIT(04/30/2011,'99$99$9999');
-SET &EDATEX = EDIT(05/06/2011,'99$99$9999');
-SET &WEEK_DIFF = (DAMDY(05062011,'I8') - DAMDY(04302011,'I8')) / 7 ;
-TYPE 0
0


WebFOCUS 7.6
Windows, All Outputs
July 21, 2011, 11:23 AM
Tom Flynn
 
-SET &ECHO=ALL;
-SET &DATE1  = '04/30/2011';
-SET &DATE2  = '05/06/2011';
-SET &SDATEX = EDIT(&DATE1,'$$$$$$9999/') | EDIT(&DATE1,'99999');
-SET &EDATEX = EDIT(&DATE2,'$$$$$$9999/') | EDIT(&DATE2,'99999');
-SET &DIFF1  = (DATEDIF('&SDATEX.EVAL', '&EDATEX.EVAL', 'D') + 1) / 7;
-TYPE &DIFF1 
-EXIT



Tom Flynn
WebFOCUS 8.1.05 - PROD/QA
DB2 - AS400 - Mainframe
July 21, 2011, 12:23 PM
Dan Satchell
I sometimes forget about my WF environment settings. For the code below to work, DMPRECISION must be set to zero so Dialogue Manager will round up instead of truncating decimals. With DMPRECISION=0, both of these examples should work.

-SET &ECHO = ALL ;
SET DMPRECISION = 0
-RUN
-SET &SDATE = '04/30/2011';
-SET &EDATE = '05/06/2011';
-SET &SDATEX = EDIT(&SDATE,'99$99$9999');
-SET &EDATEX = EDIT(&EDATE,'99$99$9999');
-SET &WEEK_DIFF = (DAMDY(&EDATEX,'I8') - DAMDY(&SDATEX,'I8')) / 7 ;
-TYPE &WEEK_DIFF


-SET &ECHO = ALL ;
SET DMPRECISION = 0
-RUN
-SET &SDATEX = EDIT('04/30/2011','99$99$9999');
-SET &EDATEX = EDIT('05/06/2011','99$99$9999');
-SET &WEEK_DIFF = (DAMDY(&EDATEX,'I8') - DAMDY(&SDATEX,'I8')) / 7 ;
-TYPE &WEEK_DIFF



WebFOCUS 7.7.05
July 21, 2011, 02:38 PM
Francis Mariani
Charles,

I feel we're flogging a dead horse here!

Do you need a solution for Dialogue Manager or for TABLE, or both?


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
July 21, 2011, 02:45 PM
Charles Richards
Thanks for all the help. This code is what managed to work for me

SET DMPRECISION = 0
-RUN
-SET &SDATEX = EDIT(&SDATE,'$$$$$$9999/') | EDIT(&SDATE,'99999');
-SET &EDATEX = EDIT(&EDATE,'$$$$$$9999/') | EDIT(&EDATE,'99999');
-SET &DIFF1 = (DATEDIF('&SDATEX.EVAL', '&EDATEX.EVAL', 'D') + 1) / 7;
-TYPE &DIFF1


Last question is how Can i use the value "&DIFF1" in a compute or define?


WebFOCUS 7.6
Windows, All Outputs
July 21, 2011, 03:37 PM
FrankDutch
DEFINE FILE XXXX

NUMBERWEEKS/I3=&DIFF1;
END

done




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

July 21, 2011, 04:46 PM
Charles Richards
Thank you everyone for all of your help!!


WebFOCUS 7.6
Windows, All Outputs
July 21, 2011, 05:54 PM
Prarie
Yea!!