Sometimes it is necessary to retrieve only part of a date. NAV has built-in functions to do just that. We will show you how to use them in this recipe.
Let's create a new codeunit from Object Designer.
Then add the following global variables by navigating to View | C/AL Globals (Alt + V + B):
Name
Type
Day
Integer
Month
Integer
Year
Integer
Write the following code into the
OnRun
trigger of the codeunit:Day := DATE2DMY(TODAY, 1); Month := DATE2DMY(TODAY, 2); Year := DATE2DMY(TODAY, 3); MESSAGE('Day: %1\Month: %2\Year: %3', Day, Month, Year);
To complete the task, save and close the codeunit.
On executing the codeunit, you should see a window similar to the following screenshot:
The Date2DMY
function is a basic feature of NAV. The first parameter is a date variable. This parameter can be retrieved from the system using TODAY
or WORKDATE
. Additionally, a hardcoded date such as 01312010D
or a field from a table, such as Sales Header
or
Order Date
can be used as a first parameter. The second parameter is an integer that tells the function which part of the date to return. This number can be 1, 2, or 3, and corresponds to the day, month, and year (DMY) respectively.
The following code can be found in the 485
, Business Chart Buffer
table in the UpdateCurrencyFactor()
method of the GetNumberOfYears()
function:
EXIT(DATE2DMY(ToDate,3) - DATE2DMY(FromDate,3));
This function has two parameters of type date and it returns the value in integer. The basic usage of this function is to calculate the duration between two dates in terms of years.