Get Number of days from from date and store them on INTEGER
tiagofrancis
Member Posts: 48
Hi Folks,
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?
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?
0
Answers
-
One way is to filter Date virtual table for the days between you two dates, filter out weekends and count the records... ;-)0
-
Hi,
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 regards0 -
My variant:
Date.SETRANGE("Period Type","Period Type"::"Date"); Date.SETRANGE("Period Start",FromDate,ToDate); Date.SETRANGE("Period No.",1,5); //Monday..Friday NoOfWorkdates := Date.COUNT;
Isn't it easy?
8)0 -
kine wrote:My variant:
Date.SETRANGE("Period Type","Period Type"::"Date"); Date.SETRANGE("Period Start",FromDate,ToDate); Date.SETRANGE("Period No.",1,5); //Monday..Friday NoOfWorkdates := Date.COUNT;
Isn't it easy?
8)
The less lines of code, the better!
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
kine wrote:My variant:
Date.SETRANGE("Period Type","Period Type"::"Date"); Date.SETRANGE("Period Start",FromDate,ToDate); Date.SETRANGE("Period No.",1,5); //Monday..Friday NoOfWorkdates := Date.COUNT;
Isn't it easy?
8)
I would have thought so. i suppose it depends how much a person enjoys typing
David Singleton0 -
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 yearNoOfDays := (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 anydayNoOfdays := (((ToDate-fromDate) +1)DIV 7) *5; Message('Option 2 Days = %1',NoOfDays);
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
DavidAnalyst Developer with over 17 years Navision, Contract Status - Busy
Mobile: +44(0)7854 842801
Email: david.cox@adeptris.com
Twitter: https://twitter.com/Adeptris
Website: http://www.adeptris.com0 -
David Cox wrote:My Variant with no need for any calls to the Database "Date" tables for this simple code

...
Date is a virtual table, so there are no calls to the database required. :-$David Singleton0
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 610 NAV Courses, Exams & Certification
- 1.9K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 251 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions


