Skip to content

How to Use Date Functions in Microsoft Dynamics Business Central On Premises How to Used Date Functions in Microsoft Dynamics 365 Business Central On Premises

There are various places throughout Microsoft Dynamics 365 Business Central where you can enter dates in fields. In fact, a good deal of data in the application is linked to dates. For example, in a sales order, you can set the shipment date. When filtering lists or report data, you can enter dates and times to pinpoint only the data you want to see. If you look at a sales order, an invoice. or a credit memo, there are document dates, posting dates, and date of entry. On the customer record, there is the date of birth. This blog explains how to use the Date function for specific uses.

When processing these dates in the code, you can use certain Date functions to get specific information out of a date, or to change a date in a specific format:

  • DATE2DMY
  • DATE2DWY
  • CALCDATE
  • Today
  • Date

There is also the Time function, which gets the current time, and the Workdate function, which gets the work date that is set in the application.

DATE2DMY

With DATE2DMY used to get specific parts out of a certain date. DATE2DMY stands for “Date To Day, Month, Year”.

Syntax: Number: = DATE2DMY(date, position);

Date: The inputting date.

Position: Specifies what the function returns. The valid options are 1, 2, and 3.

The value 1 corresponds to day of the week (1-7, Monday = 1).

The value 2 corresponds to week number (1-53).

The value 3 corresponds to year.

To use this function, create a new file for the codeunit. Be sure to use a standard file name format for files. To create the codeunit, use the shortcut or Snippet (tc). You will be presented with many options; select tcodeunit and provide the unique codeunit ID and name.

Figure 1 – Creating a new file for a codeunit and by using snippet in Dynamics Business Central on premises
Figure 1 – Creating a new file for a codeunit and by using snippet in Dynamics Business Central on premises

To get the specific part of a date, use DATE2DMY, and to display the output, use a message and a variable to store the output. A variable can be defined with a global or a local scope. If you define your variable with a global scope, it will be available everywhere in the object. So, on every trigger, everywhere you want to write code statements, that variable will be available. When the scope is local, you can only access that variable in the specific trigger where it declared. Here, you can use the mydate variable, which is global and can be accessed everywhere.

Figure 2 – Declaring variables and assigning those values to message statements / using date Functions in Dynamics Business Central on premises
Figure 2 – Declaring variables and assigning those values to message statements / using date Functions in Dynamics Business Central on premises

This example uses the Today function to get the current date. The integer represents which specific part of the date is presented. If it is one, it shows the date; if 2, it is the month. The function’s output is assigned to the variable Mydate for the display. Then the value is displayed using a message.

Create a new file for the page extension. Be sure to use a standard file name format for files. For creating the page extension, use the shortcut or Snippet (tpage). You are presented with several options; select tpageext.

Figure 3 – Creating a Page extension by using snippet (As shown below) in Dynamics Business Central on premises
Figure 3 – Creating a Page extension by using snippet (As shown below) in Dynamics Business Central on premises

The Page Extension tells you where you want to extend action. In this example, we are extending to a salesperson list.

Figure 4 – Creating a page extension (assigning the variable to codeunit and codeunit name) in Dynamics Business Central on premises
Figure 4 – Creating a page extension (assigning the variable to codeunit and codeunit name) in Dynamics Business Central on premises

You can publish it using F5 in keyboard or the shortcut Ctrl + G to select > AL: Publish.

Figure 5 –Navigating through the option in Action to choose Mydateunit in Dynamics Business Central on premises
Figure 5 –Navigating through the option in Action to choose Mydateunit in Dynamics Business Central on premises

Figure 6 –Message to display the specified part of date in Dynamics Business Central on premises
Figure 6 –Message to display the specified part of date in Dynamics Business Central on premises

DATE2DWY

With DATE2DWY, you can get specific parts out of a certain date. DATE2DWY stands for “Date To Day, Week, Year”.

Syntax: Number: = DATE2DWY(date, position);

Date : The inputting date.

Position : Specifies what the function returns. The valid options are 1, 2, and 3.

The value 1 corresponds to day of the week (1-7, Monday = 1).

The value 2 corresponds to week number (1-53).

The value 3 corresponds to year.

Create a new file for the codeunit or use the previously created one for the dateunit. Be sure to use a standard file name format for files. To create the codeunit, use the shortcut or Snippet (tc). You will be presented with many options; select tcodeunit and provide the unique codeunit ID and name.

Figure 7 – Creating a new file for a codeunit and by using snippet in Dynamics Business Central on premises
Figure 7 – Creating a new file for a codeunit and by using snippet in Dynamics Business Central on premises

To get the specific part of a date, use DATE2DWY, and for displaying the output, use a message and a variable to store the output. A variable can be defined with a global or a local scope. If you define your variable with a global scope, it will be available everywhere in the object. So, on every trigger, everywhere you want to write code statements, that variable will be available. When the scope is local, you can only access that variable in the specific trigger where it was declared. Here, you can use mydate variable, which is global and can be accessed everywhere.

Figure 8 –Result of DATE2DWY is assigning to a variable in Dynamics Business Central on premises
Figure 8 –Result of DATE2DWY is assigning to a variable in Dynamics Business Central on premises

In Date2DWY, the position returns 3 options:

(1) the date corresponding to day of the week (1-7, Monday=1)

(2) week number (1-53)

(3) year

Figure 9 –Different returns in DATE2DWY in Dynamics Business Central on premises
Figure 9 –Different returns in DATE2DWY in Dynamics Business Central on premises

Next, create a pageextension if you are creating a new codeunit. You can use the snippet(tpage) or shortcut. You are presented with several options; select tpageext.  If you use the existing codeunit, then create the corresponding action of the new one.

Figure 10 –Creating page extension using snippet in Dynamics Business Central on premises
Figure 10 –Creating page extension using snippet in Dynamics Business Central on premises

Page Extension tells you where you want to extend action. Here we are extending to a salesperson list.

Figure 11 – Creating a page extension (assigning the variable to codeunit and codeunit name) in Dynamics Business Central on premises
Figure 11 – Creating a page extension (assigning the variable to codeunit and codeunit name) in Dynamics Business Central on premises

You can publish it using F5 on the keyboard or the shortcut Ctrl + G to select > AL: Publish.

Figure 12 –Navigating through the option in Action to choose Mydateunit in Dynamics Business Central on premises
Figure 12 –Navigating through the option in Action to choose Mydateunit in Dynamics Business Central on premises

Figure 13 –Messages that display the output in Dynamics Business Central on premises
Figure 13 –Messages that display the output in Dynamics Business Central on premises

CALCDATE

CALCDATE calculates a new date that is based on a date expression and a reference date.

Syntax:

NewDate :=   System.CalcDate(DateExpression: DateFormula, [Date: Date])

DateExpression

Type dateformula

D | WD | W | M | Q | Y (D=day, WD=weekday, W=week, M=month, Q=quarter, Y=year)

Examples of terms:

  • 30D (corresponds to30 days;)
  • WD2 (corresponds to weekday number 2)
  • CW (corresponds to current week;)

Date
 Type: Date
Use this optional parameter to define a reference date. The default is the current system date. If you omit this optional value, the current system date is used.

Create a new file for the codeunit or use the previously created one for the dateunit. Be sure to use a standard file name format for files. To create the codeunit, use the shortcut or Snippet (tc). You will be presented with many options; select tcodeunit and provide the unique codeunit id and name.

Figure 14 – Creating a new file for a codeunit and by using snippet in Dynamics Business Central on premises
Figure 14 – Creating a new file for a codeunit and by using snippet in Dynamics Business Central on premises

With the CALCDATE function, you can calculate new dates, starting from a certain date. Here we have 3 variables Expr3,Rdate and Mydate. Expr3 specifies how many days are added to current Month. RDate refers a date. Mydate variable is used to assign the calculated date.

Figure 15 –Globally declared variables and specifies the id and name of codeunit in Dynamics Business Central on premises
Figure 15 –Globally declared variables and specifies the id and name of codeunit in Dynamics Business Central on premises

Figure 16 – calc function in Dynamics Business Central on premises
Figure 16 – calc function in Dynamics Business Central on premises

You can now display the calcdate function using the message function.

Figure 17 –Navigating through the option in Dynamics Business Central on premises
Figure 17 –Navigating through the option in Dynamics Business Central on premises

Figure 18 – calculated date display using Message function in Dynamics Business Central on premises
Figure 18 – calculated date display using Message function in Dynamics Business Central on premises

TODAY and DATE

The TODAY is another date function that is used to get the current date from the operating system. The TIME function works on the same way; it provides the current time from the operating system.

You can create a new codeunit or can do the coding in the previously created codeunit. If you are creating a new codeunit, use the shortcut or Snippet (tc). You will be presented with many options; select tcodeunit and provide the unique codeunit id and name.

Declare the variable to hold values from function which is using in the display purpose.

Figure 19 –Time and Today Function in Dynamics Business Central on
Figure 19 –Time and Today Function in Dynamics Business Central on premises

The result is presented with the help of a message function.

Figure 20 – Message to display Time and Today Function in Dynamics Business Central on premises
Figure 20 – Message to display Time and Today Function in Dynamics Business Central on premises

If you have any questions about Microsoft Dynamics NAV or Business Central functionality, contact ArcherPoint.

Read more "How To" blogs from ArcherPoint for practical advice on using Microsoft Dynamics Business Central or NAV. If you are interested in NAV/Business Central development, check out our collection of NAV Development Blogs.

Read ArcherPoint's Blog Follow us on Twitter Follow us on Facebook Follow us on LinkedIn Link to our RSS feed Watch us on YouTube
Get Help Now