Norbert Eckert Posted May 11, 2021 Posted May 11, 2021 Hi All, Im futzing with the DATEDIF function to get the working business days in a month. My holiday file has no entries for May 2021 so I would expect to get 21 days. Testing this gives 20 days instead. Similarly I got 21 days for April. This whacks things up when you actually have holidays in the month. Does anyone know what could be incorrect Thanks, Norb FILEDEF HDAYSBLC DISK /ibi/apps/jobstart/hdaysblc.err -RUN -* setup the business week (Mon - Fri) and the holiday file to use SET BUSDAYS = MTWTF SET HDAY = SBLC -RUN -SET &MONTHSTART = DATECVT( DATEMOV( DATECVT(&YYMD,I8YYMD,YYMD),BOM),YYMD,I8YYMD); -SET &MONTHEND = DATECVT( DATEMOV( DATECVT(&YYMD,I8YYMD,YYMD),EOM),YYMD,I8YYMD); -SET &WORKINGDAYSINMONTH = DATEDIF(DATECVT(&MONTHSTART, I8YYMD, YYMD), DATECVT(&MONTHEND, I8YYMD, YYMD), BD); -RUN -TYPE &MONTHSTART &MONTHEND &WORKINGDAYSINMONTH
David Briars Posted May 12, 2021 Posted May 12, 2021 -* -* Environmental Settings -* SET BUSDAYS = _MTWTF_ -* -* Find Number of Business Days in a Given Year/Month -* DEFINE FUNCTION BD_MON (YEAR/I4,MONTH/I2) -* Year/Month - Numeric THIS_IYYM/I6YYM = (YEAR * 100) + MONTH; -* Year/Month/Day - First Day of the Month BOM_YYMD/YYMD = THIS_IYYM; -* Difference between the first day of this month and the first day of next month -* in Business Days. BD_MON/I2 = DATEDIF( BOM_YYMD, DATEMOV(BOM_YYMD,'EOM') + 1, 'BD'); END -RUN -* -* How many business days are in the current month -* -SET &YEAR = EDIT(&YYMD,'9999'); -SET &MONTH = EDIT(&YYMD,'$$$$99'); -SET &WORK = BD_MON(&YEAR,&MONTH); -TYPE There are &WORK business days in the current month (&YYMD). I think the issue in your code is that you are finding the difference between the first and last day of the month, and you need to find the difference between the first day of the current month, and the first day of the next. Hence the + 1 you see in my DATEDIF. I will use DEFINE FUNCTIONs for Dialogue Manager date math to make the conversions between alpha and date formats a little more straightforward. Also, often, my DEFINE FUNCTIONs will be -INCLUDEd and perhaps shared between DM and TABLE commands if needed.
John Gelona Posted May 12, 2021 Posted May 12, 2021 David is correct, if you need to know the number of Business Days in a given month, you need to subtract the first day of the month from the first day of the next month. Based on your code, the answers you got are correct. The real head scratcher comes when one of the dates is a holiday. For example, add May 31st 2021 to your holiday file, now do the DATEDIF the way youve done it then do it with June 1st. Can you figure out what undocumented feature is in play And dont even get me started with how DATEDIF and HDIFF can return different answers for the same dates.
Norbert Eckert Posted June 8, 2021 Author Posted June 8, 2021 Thank you, gentlemen, for your replies. After futzing with the DATEDIF function and the calendar file I reached the same conclusion that you did and have modified my code accordingly. -* Need to add a day to the last day of the month because the DATEDIF function counts the start date but not the end date. -SET &CURRMONEND_MOD = DATECVT( DATEADD( DATECVT(&DTE,I8YYMD,YYMD),D, 1),YYMD,I8YYMD); -SET &WORKINGDAYSINMONTH = DATEDIF(DATECVT(&DTS, I8YYMD, YYMD), DATECVT(&CURRMONEND_MOD, I8YYMD, YYMD), BD);
David Beagan Posted June 9, 2021 Posted June 9, 2021 I wonder if you could simplify your code by adding 1 to the converted &DTE like this. -SET &WORKINGDAYSINMONTH = DATEDIF(DATECVT(&DTS, 'I8YYMD', 'YYMD'), 1 + DATECVT(&DTE, 'I8YYMD', 'YYMD'), 'BD');
Norbert Eckert Posted July 12, 2021 Author Posted July 12, 2021 David Beagan, Your code simplification does indeed work and Ill put it into the fex. I seem to recall that Ive been burned in some circumstances when adding or subtracting with smart dates. So I have avoided that technique but well see how this goes. Thanks.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now