Monday, March 7, 2016

How To Calculate Difference Between Two Dates in Navision 2013 R2 and Higher ?

Hi All,

It was always been a trouble to calculate Difference Between Two Date Time Variables.

With Microsoft Dynamics NAV 2013 R2, Dotnet Variable are available which make life easy in so many situations. Today we will discuss one of the Dotnet Component for calculation Difference between days.

This Solution can be implemented on Navision Version  2013 R2 and Later. The Demo Steps are on NAV 2016 W1 Database.



1. Let's Create a Page Date Demo, with Two Input Variables of StartDateTime & EndDateTime of Type DateTime.

2. The Page Should Look Like as below.



3. I am Creating three Dotnet Variables as below -

DateandTime  - Microsoft.VisualBasic.DateAndTime.'Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

DayOfWeekInput - Microsoft.VisualBasic.FirstDayOfWeek.'Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

WeekOfYearInput - Microsoft.VisualBasic.FirstWeekOfYear.'Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

4. Actually The First Variable Does the Magic what we want. So let's understand the use of Other Two.

DayOfWeekInput argument can have one of the following settings  - 
 0 - First day of week specified in system settings
 1 - Sunday (default)
 2 - Monday (complies with ISO standard 8601, section 3.17)
 3 - Tuesday
 4 - Wednesday
 5 - Thursday
 6 - Friday
 7 - Saturday

WeekOfYearInput argument can have one of the following settings - 
 0 - First week of year specified in system settings
 1 - Week in which January 1 occurs (default)
 2 - Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
 3 - First full week in the new year

** I will be Using Default for both the parameters in the demo. 

You can change it based on your need.For Changing These You can just call the Enumerators as per your need from the C/AL Symbol Menu as shown Below.


5. Now let's see how our DateandTime , dotnet variable fullfill the requirement which we are looking for.

- If you Check the Functions that this variable have in C/AL Symbol Menu, you will find a function DateDiff, Which will return the Difference between two date time variables, as below -


6. Now we know all marked as Green in our Above Image, but still the first parameter which is red is mystery.

The First Parameter i.e Date Interval, will decide what you want in Result. You Might ways difference, in months, days, Quarter, Year, Minutes, Seconds, Hours... and many more..

The Dotnet Class Give You 10 Options In which You can Get the Object. Below is the list of Options that you have -

- Day              
- Day Of Year
- Hour            
- Minute
- Month          
- Quarter
- Second
- Week
- Calendar week
- Year

7. For the Demo, I will be creating 10 integer type variables for showing output in all result types, as below - 
** Add all these variables in a Seprate Group in Page.


 8. I Will Create a Function To Set and Reset these Variables which will be called on Validate of My Two Input date Variables as shown below -


9. Let's Run the Page and See output of difference between two date time variables.


References - MSDN

Hope You find the demo useful. You can use any of the command in the above list as per your requirement in the C/AL code.

If required You can download the Date Demo Objects from Dynamics User Group OR my Skydrive.

File Name - Microsoft Dynamics NAV 2016 - Saurav-NAV.19.00.DateDemo

Regards,
+saurav dhyani
saurav-nav.blogspot.com

No comments:

Post a Comment