Get Number of days from from date and store them on INTEGER

tiagofrancistiagofrancis 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?

Answers

  • kinekine Member Posts: 12,562
    One way is to filter Date virtual table for the days between you two dates, filter out weekends and count the records... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • waliserwaliser Member Posts: 4
    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 regards
  • kinekine Member Posts: 12,562
    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)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • tinoruijstinoruijs Member Posts: 1,226
    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)

    :mrgreen:
    The less lines of code, the better!

    Tino Ruijs
    Microsoft Dynamics NAV specialist
  • David_SingletonDavid_Singleton Member Posts: 5,479
    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 :mrgreen:
    David Singleton
  • David_CoxDavid_Cox Member Posts: 509
    My Variant with no need for any calls to the Database "Date" tables for this simple code :lol:
    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
    NoOfdays := (((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

    David
    Analyst 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.com
  • David_SingletonDavid_Singleton Member Posts: 5,479
    David Cox wrote:
    My Variant with no need for any calls to the Database "Date" tables for this simple code :lol:

    ...

    Date is a virtual table, so there are no calls to the database required. :-$
    David Singleton
Sign In or Register to comment.