Calculating Date Difference in Days

Kitui
Member Posts: 9
Iam doing a report that classifies loans based on the last payment day and today.If last payment day is less than a month it is classified as performing.Did the following code but an error pops 'Date is Invalid' when i run the report.
Day1:=DATE2DMY("Last Pay Date",1);
Month1:=DATE2DMY("Last Pay Date",2);
Year1:=DATE2DMY("Last Pay Date",3);
Day2:=DATE2DMY(TODAY,1);
Month2:=DATE2DMY(TODAY,2);
Year2:=DATE2DMY(TODAY,3);
IF (Year1=Year2) AND ((Month2-Month1)<=1) AND (( Day2-Day1)<30) THEN
"Loans Register"."Loans Category":="Loans Register"."Loans Category"::Perfoming
ELSE
Day1:=DATE2DMY("Last Pay Date",1);
Month1:=DATE2DMY("Last Pay Date",2);
Year1:=DATE2DMY("Last Pay Date",3);
Day2:=DATE2DMY(TODAY,1);
Month2:=DATE2DMY(TODAY,2);
Year2:=DATE2DMY(TODAY,3);
IF (Year1=Year2) AND ((Month2-Month1)<=1) AND (( Day2-Day1)<30) THEN
"Loans Register"."Loans Category":="Loans Register"."Loans Category"::Perfoming
ELSE
0
Best Answers
-
The DATE2DMY throws such an error when the date parameter passed to it is empty (0D). The "Last Pay Date" must be empty in one of records you are processing.
You can simplify your code like this:IF "Last Pay Date" > CALCDATE('<-1M>', TODAY) THEN "Loans Register"."Loans Category":="Loans Register"."Loans Category"::Perfoming ELSE //non-performing
that will go to ELSE section if the "Last Pay Date" is blank. And it works when dates crosses a year boundary.Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035 -
If you keep your code and add filter excluding records with empty "Last Pay Date" it will not throw any errors, but it will incorrectly classify loans when your code will be running in January - every year;
Doing calculations directly on TODAY's and "Last Pay Date" field prevents you from doing any real testing. What you should do is to create a function with two dates as input parameters, and boolean as output, and encapsulate calculations in this function:IsLoanPerforming(PaymentDate Date, TestDate Date) : Boolean { IF TestDate = 0D THEN EXITFALSE); //or return TRUE or ERROR incorrect input parameter - whatever rule suits your busines EXIT( PaymentDate > CALCDATE('<-1M>', TestDate) ); }
or if you wish to keep your code:IsLoanPerforming(PaymentDate Date, TestDate Date) : Boolean { Day1:=DATE2DMY("Last Pay Date",1); Month1:=DATE2DMY("Last Pay Date",2); Year1:=DATE2DMY("Last Pay Date",3); Day2:=DATE2DMY(TODAY,1); Month2:=DATE2DMY(TODAY,2); Year2:=DATE2DMY(TODAY,3); EXIT( (Year1=Year2) AND ((Month2-Month1)<=1) AND (( Day2-Day1)<30) ); }
This way you can make your code more understandable :IF IsLoanPerforming("Last Pay Date", TODAY) THEN "Loans Register"."Loans Category":="Loans Register"."Loans Category"::Perfoming ELSE //non-performing
and also you could test a few different date combinations to make sure the code works in all casessomePaymentDate ;= 01012018; someTestDate := 02012018; MESSAGE( 'Payment date:%1, Test Date: %2, Loan Performing : %3', somePaymentDate , someTestDate, IsLoanPerforming(somePaymentDate, someTestDate) ); somePaymentDate ;= 31122017; someTestDate := 02012018; MESSAGE( 'Payment date:%1, Test Date: %2, Loan Performing : %3', somePaymentDate , someTestDate, IsLoanPerforming(somePaymentDate, someTestDate) )
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035 -
[Topic moved from 'General Chat' forum to 'NAV Three Tier' forum]
Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!5
Answers
-
The DATE2DMY throws such an error when the date parameter passed to it is empty (0D). The "Last Pay Date" must be empty in one of records you are processing.
You can simplify your code like this:IF "Last Pay Date" > CALCDATE('<-1M>', TODAY) THEN "Loans Register"."Loans Category":="Loans Register"."Loans Category"::Perfoming ELSE //non-performing
that will go to ELSE section if the "Last Pay Date" is blank. And it works when dates crosses a year boundary.Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035 -
And if i setfilter where last pay day isnt nill.0
-
If you keep your code and add filter excluding records with empty "Last Pay Date" it will not throw any errors, but it will incorrectly classify loans when your code will be running in January - every year;
Doing calculations directly on TODAY's and "Last Pay Date" field prevents you from doing any real testing. What you should do is to create a function with two dates as input parameters, and boolean as output, and encapsulate calculations in this function:IsLoanPerforming(PaymentDate Date, TestDate Date) : Boolean { IF TestDate = 0D THEN EXITFALSE); //or return TRUE or ERROR incorrect input parameter - whatever rule suits your busines EXIT( PaymentDate > CALCDATE('<-1M>', TestDate) ); }
or if you wish to keep your code:IsLoanPerforming(PaymentDate Date, TestDate Date) : Boolean { Day1:=DATE2DMY("Last Pay Date",1); Month1:=DATE2DMY("Last Pay Date",2); Year1:=DATE2DMY("Last Pay Date",3); Day2:=DATE2DMY(TODAY,1); Month2:=DATE2DMY(TODAY,2); Year2:=DATE2DMY(TODAY,3); EXIT( (Year1=Year2) AND ((Month2-Month1)<=1) AND (( Day2-Day1)<30) ); }
This way you can make your code more understandable :IF IsLoanPerforming("Last Pay Date", TODAY) THEN "Loans Register"."Loans Category":="Loans Register"."Loans Category"::Perfoming ELSE //non-performing
and also you could test a few different date combinations to make sure the code works in all casessomePaymentDate ;= 01012018; someTestDate := 02012018; MESSAGE( 'Payment date:%1, Test Date: %2, Loan Performing : %3', somePaymentDate , someTestDate, IsLoanPerforming(somePaymentDate, someTestDate) ); somePaymentDate ;= 31122017; someTestDate := 02012018; MESSAGE( 'Payment date:%1, Test Date: %2, Loan Performing : %3', somePaymentDate , someTestDate, IsLoanPerforming(somePaymentDate, someTestDate) )
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035 -
[Topic moved from 'General Chat' forum to 'NAV Three Tier' forum]
Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!5
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K 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
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions