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

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 - 

Error After Restore SQL Backup of NAV 2013 Database

Hi all, we are facing a conman issue during restoring a SQL Database backup restored for NAV 2013. While Trying to run object from Object Designer we get below listed error (even the service is configured properly) - --------------------------- Microsoft Dynamics NAV Development Environment --------------------------- There are no NAV Server instances available for this database. You must ensure that a NAV Server instance is running and is configured to use the database before you perform this activity. --------------------------- OK    ---------------------------

Future - What Next After NAV 2016. Code Name - Madeira...

Hi All, While Browsing web I came across some article which says about next release of Microsoft Dynamics NAV. Let's see the History of Code Names Given to Previous Version  - NAV 2013 - Code Name 'Sicily' NAV 2015 - Code Name 'CRETE' NAV 2016 - Code Name 'CORFU' And as we all know that all these are the Islands.