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=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

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

WeekOfYearInput - Microsoft.VisualBasic.FirstWeekOfYear.'Microsoft.VisualBasic, Version=, 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

+saurav dhyani


Popular posts from this blog

Welcome - Microsoft Dynamics 365!

Hi All, In this article we will discuss the new introduced member of Dynamics Family. Yes you heard it correctly, one more new member with Name "Microsoft Dynamics 365" . #ProjectMadeira   #Dynamics365

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 - 

NAV 2013 Credential Type - How to Use?

Hi all, Please do these activities when you have time, its a long activity and i would suggest do all activities in one go. So be clam and lets start. For NAV 2013 R2 Refer Here. For NAV 2016 Steps have been Reduced Please Refer Here. I have installed NAV 2013 and its work great with my windows credentials. I was checking the service console (Microsoft Dynamics NAV Administration) in NAV 2013 and found out that Credentials Type have multiple Options.