I'm trying to subtract to dates and stored them on an integer.
What is the best way to do it regarding that on those days i have to subtract the weekend?
My Variant with no need for any calls to the Database "Date" tables for this simple code
Fromdate := 010108D;
todate := TODAY;
Option #1 MOD and DIV are your Friends
DIV Returns a whole Number of Divisions
MOD returns the remainder
This will give you Weeks and Days
Remember 1st of January was a Tuesday this year
NoOfDays := (ToDate-fromDate)+1;
NoOfWeeks := NoOfDays DIV 7;
NoOfDays := NoOfDays MOD 7;
MESSAGE(STRSUBSTNO('Weeks %1 and Days %2',NoOfWeeks,NoOfDays));
Option #2
Get the Number of Full Weeks * 5 days only 1 line of Code
This can start on anyday to anyday
Option #3 we are looking at only full Monday to Fridays,
//Get the Number of Full Weeks Monday - Sunday including part weeks as whole weeks
NoOfweeks := ((CALCDATE('CW',ToDate)-CALCDATE('-CW',fromDate))+1) DIV 7;
//OPTIONAL CODE START >>
//This next code is not so good because a Tuesday to a Thurday will lose 8 working days
//Exclude Part Week if not a Monday
IF DATE2DWY(FromDate, 1) <> 1 THEN
NoOfweeks := NoOfweeks - 1;
//Exclude Part Week - Less than a Friday
IF DATE2DWY(ToDate, 1) < 5 THEN
NoOfweeks := NoOfweeks - 1;
//OPTIONAL CODE END <<
//The Number of Full Weeks * 5 Days
IF NoOfweeks > 0 THEN
NoOfDays := NoOfWeeks * 5
ELSE
NoOfDays = 0;
Message('Option 3 Days = %1',NoOfDays);
Option #4 look at the code and learn, change it to what you want to do, post your solution here for others
Answers
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
I suggest this solution:
TEXT CONSTANTS
============
Name ConstValue
Text001 There are %1 Workdays between %2 and %3
VARIABLES
=======
Name DataType Subtype Length
StartingDate Date
EndingDate Date
WorkdaysBetween Integer
TempDate Date
DayOfWeek Integer
CODE
====
StartingDate := 010308D;
EndingDate := 160408D;
TempDate := StartingDate;
WHILE TempDate <= EndingDate DO BEGIN // <= Includes EndingDate
//WHILE TempDate < EndingDate DO BEGIN // < Excludes EndingDate
DayOfWeek := DATE2DWY(TempDate, 1);
IF DayOfWeek <= 5 THEN // Monday - Friday
WorkdaysBetween := WorkdaysBetween + 1;
TempDate := CALCDATE('<+1D>', TempDate);
END;
MESSAGE(Text001, WorkdaysBetween, StartingDate, EndingDate);
should work ...
Best regards
Isn't it easy?
8)
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
The less lines of code, the better!
Tino Ruijs
Microsoft Dynamics NAV specialist
I would have thought so. i suppose it depends how much a person enjoys typing
Option #1 MOD and DIV are your Friends
DIV Returns a whole Number of Divisions
MOD returns the remainder
This will give you Weeks and Days
Remember 1st of January was a Tuesday this year
Option #2
Get the Number of Full Weeks * 5 days only 1 line of Code
This can start on anyday to anyday
Option #3 we are looking at only full Monday to Fridays,
Option #4 look at the code and learn, change it to what you want to do, post your solution here for others
David
Mobile: +44(0)7854 842801
Email: david.cox@adeptris.com
Twitter: https://twitter.com/Adeptris
Website: http://www.adeptris.com
Date is a virtual table, so there are no calls to the database required. :-$