Skip to main content

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

Comments

Popular posts from this blog

BC 21 and Higher - PowerShell Cmdlet (Replacement of Business Central Administration).

Hi Readers, As discussed in last article about deprecating of Business Central Administration, there are few common actions that we use in administration till Business Central 20. For our on-prem customers, we will still require doing activities. As Microsoft suggest we need to start using PowerShell cmdlet.    Let's see how to do those via PowerShell, or Administration Shell. I will be keep adding commands as you comment to this article.

Send Mail with Attachment From Navision.

Hi all, We have seen how to save a report into PDF and how to send mail to a customer. Let's link these two post in one i.e. Mailing statement to a customer into PDF Format. This article is part of the Series. Please Refer  Table of Content here . If you have the old objects set let me brief you what I will be changing - 

MSDYN365BC - Data Upgrade To Microsoft Dynamics 365 Business Central on premises.

Hi Readers, We have already talked about the number of steps for upgrading to Business Central on Premises from different NAV versions. After that article, I received multiple requests for an article which list down steps for Data Migration. In this article, we will discuss steps of data migration to MSDYN365BC (on-Prem) from NAV 2017. For this article, I am considering a Cronus Demo Database without any customization. For an actual upgrade project, we will have to complete object merge using compare and Merge process. After the Merge Process, the next step is data migration. Let's discuss those steps. Direct Upgrade to Microsoft Dynamics 365 Business Central (on-Prem) is from following versions - 1. NAV 2015. 2. NAV 2016. 3. NAV 2017. 4. NAV 2018.