Focal Point
[SOLVED] Use variables dates in DATEDIF functions

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

May 20, 2011, 01:27 AM
anrix
[SOLVED] Use variables dates in DATEDIF functions
Hi,

I try to get the number of days between 2 dates that my report ask to my users, but always I received a "0" value.

This is where I make my WHERE questions:

WHERE ( REPARTOCIRCULACION.ENTREGAFECHA GE '&ENTREGAFECHA' ) AND
( REPARTOCIRCULACION.ENTREGAFECHA LE '&ENTREGAFECHA2' );


This is my COMPUTE where I use variables dates:

COMPUTE Numdias/I3 = DATEDIF(&ENTREGAFECHA, &ENTREGAFECHA2, 'D');

Is valid to use variable dates in the DATEDIF function?

how I could I get the number of days of the lapse of time that my users select in the report?

Thanks for your help!

greetings!
Tony

This message has been edited. Last edited by: Kerry,


WebFocus 7.6.10, windows 7, win server 2003-2008 PDF, TXT, XML, html, xls, doc
May 20, 2011, 04:56 AM
websmash
Hi
just try to add quotes to the vars
COMPUTE Numdias1/I3 = DATEDIF('&ENTREGAFECHA', '&ENTREGAFECHA2', 'D');

greetings
Andy
May 20, 2011, 09:49 AM
Emily Max
Hi Tony,
Dates in WebFocus can be a little tricky. You have to use an I8 format for date function. So you will first need to convert your &variable dates and then use the datedif function.
If your users use a mm/dd/yyyy format, the following code will work.

-DEFAULT &begindate = '06/01/2009';
-DEFAULT &enddate = '12/31/2009';

-SET &beginfmt = DATECVT(EDIT(&begindate,'99$99$9999'),'A8MDYY','I8');
-SET &endfmt = DATECVT(EDIT(&enddate,'99$99$9999'),'A8MDYY','I8');
-SET &total_days = DATEDIF(&beginfmt, &endfmt,'D');

The EDIT in the date convert function simply strips the '/' characters out of the user entered date.

Happy Friday Smiler

Emily


WF 8.1.05 on Windows machines
Backend: Informix, SQL and Oracle databases
May 20, 2011, 11:56 AM
anrix
Thanks for your answers, but How i could use via my DataFile?

(add quotes to the vars doesn´t work)

Greetings!
Tony


WebFocus 7.6.10, windows 7, win server 2003-2008 PDF, TXT, XML, html, xls, doc
May 20, 2011, 01:44 PM
FrankDutch
Try this

COMPUTE Numdias/I3 = DATEDIF('&ENTREGAFECHA.EVAL', '&ENTREGAFECHA2.EVAL', 'D');


this might solve you problem

did you also add
- SET &ECHO=ALL;

at the start of your program?

and now alter the output format to an hold file and see what the echo shows you?

The suggestion of Emily could also help you to show what the user did type in for the begin en enddate amper values.




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

May 21, 2011, 08:33 AM
George Patton
There appears to be a small typo here:

quote:

WHERE ( REPARTOCIRCULACION.ENTREGAFECHA GE '&ENTREGAFECHA' ) AND
( REPARTOCIRCULACION..ENTREGAFECHA LE '&ENTREGAFECHA2' );

- two periods between the SEGNAME and FIELDNAME -

I assume because you are still testing that you are entering the dates in a consistent format, but as Emily points out you may have to strip out unwanted characters and re-arrange if you allow users to type in the dates. The only useful date format is YYMD (which happens to be the SI standard, so why the heck do people still write MM/DD/YY ! ... grrrrr). When you get into production I would strongly suggest you only allow your users to use the calendar picker, so the dates automatically get returned as YYMD format and the users can't 'muck' up by putting in dates wrong.

George


WebFOCUS 7.7.05 Windows, Linux, DB2, IBM Lotus Notes, Firebird, Lotus Symphony/OpenOffice. Outputs PDF, Excel 2007 (for OpenOffice integration), WP
May 23, 2011, 03:53 AM
GamP
I tested this and it works as expected:
-SET &ENTREGAFECHA  = '2009/06/09';
-SET &ENTREGAFECHA2 = '2010/06/09';

TABLE FILE CAR
PRINT COUNTRY
COMPUTE Numdias/I8 = DATEDIF('&ENTREGAFECHA', '&ENTREGAFECHA2', 'D');
END
It does not matter if the dates are with or without slashes, it just works. Try the above code and see if it works for you also.


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
May 23, 2011, 05:06 PM
Waz
What format are your dates in ?

The database one and the amper variables.

REPARTOCIRCULACION.ENTREGAFECHA
&ENTREGAFECHA
&ENTREGAFECHA2


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

May 29, 2011, 11:14 PM
anrix
Originally my field looks like this: DMYY (across Latin America use this format.) I Change the format as suggested by a DEFINE: entregafecha / YYMD = REPARTOCIRCULACION. ENTREGAFECHA;
Subsequently use the following question:

WHERE (REPARTOCIRCULACION.entregafecha GE '& entregafecha') AND (REPARTOCIRCULACION.entregafecha LE '& entregafecha2');
I put a calendar control for my users to choose the date

and ended up using the function:

COMPUTE Numdias/I3 = DATEDIF (& entregafecha, & entregafecha2, 'D');

When I run the report, I used the dates:

entregafecha = 2011/05/05 and entregafecha2 = 2011/05/09

and the result in Numdias is equal to "-36"

This message has been edited. Last edited by: anrix,


WebFocus 7.6.10, windows 7, win server 2003-2008 PDF, TXT, XML, html, xls, doc
May 30, 2011, 03:22 AM
FrankDutch
this should work

-SET &ECHO=ALL;

-PROMPT &STARTDATE_DDMMYYYY.Give the startdate.
-PROMPT &ENDDATE_DDMMYYYY.Give the end date.
-DEFAULT &STARTDATE_DDMMYYYY=11052011;
-DEFAULT &ENDDATE_DDMMYYYY=19052011;
-SET &DAY1=EDIT(&STARTDATE_DDMMYYYY,'99$$$$$$');
-SET &MAAND1=EDIT(&STARTDATE_DDMMYYYY,'$$99$$$$');
-SET &YEAR1=EDIT(&STARTDATE_DDMMYYYY,'$$$$9999');
-SET &STARTDATE=&YEAR1||&MAAND1||&DAY1;
-TYPE &STARTDATE
-SET &DAY2=EDIT(&ENDDATE_DDMMYYYY,'99$$$$$$');
-SET &MAAND2=EDIT(&ENDDATE_DDMMYYYY,'$$99$$$$');
-SET &YEAR2=EDIT(&ENDDATE_DDMMYYYY,'$$$$9999');
-SET &ENDDATE=&YEAR2||&MAAND2||&DAY2;
-TYPE &ENDDATE

DEFINE FILE CAR
NUMDAYS/I10 WITH CAR = DATEDIF('&STARTDATE','&ENDDATE','D');
END
TABLE FILE CAR
PRINT NUMDAYS
END




It is not the most elegant way, but I used it many years ago when we did not have so much functions to translate.

It does not handle any typing errors




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

May 30, 2011, 10:18 AM
GamP
quote:
and the result in Numdias is equal to "-36"

As websmash already said in his first reply to this, you have to add quotes around the variables. If you don't you'll get -36 as a result, if you do, you get 4.


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
May 30, 2011, 10:55 AM
anrix
Perfect!

is working properly!

thank you very much to all who answered!

Greetings from Mexico!

Tony


WebFocus 7.6.10, windows 7, win server 2003-2008 PDF, TXT, XML, html, xls, doc