Dynamics 365 Business Central: How to use Date and Time Functions (Get Today, Yesterday, Tomorrow, First and Last day of the month…)

Dynamics 365 Business Central

Hi, Readers.
Yesterday, a friend asked me how to quickly get some special date in AL, such as yesterday ,tomorrow, first and last day of the month. So in this post, I will discuss some tips about date calculation.
Let’s first take a look at the basic methods of date calculation in Business Central. (Some samples are from Microsoft DOCS)

1. Today Method: Gets the current date set in the operating system.
Example:

On the regional format set to English (United States), the message window could display the following:

2. Time Method: Gets the current time from the operating system.
Example:

On the regional format set to English (United States), the message window could display the following:

3. WorkDate Method: Gets and sets the work date for the current session.


Example:

On the regional format set to English (United States), the message window could display the following:

4. CurrentDateTime Method: Gets the current DateTime.
Example:

On the regional format set to English (United States), the message window could display the following:

5. Date2DMY Method: Gets the day, month, or year of a Date Data Type.
Number := System.Date2DMY(Date: Date, Value: Integer)
The value 1 corresponds to day
The value 2 corresponds to month
The value 3 corresponds to year
Example:

The message window displays the following:

6. Date2DWY Method: Gets the day of the week, week number, or year of a Date Data Type.
Number := System.Date2DWY(Date: Date, Value: Integer)
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.
Example:

The message window displays the following information:

7. CalcDate Method: Calculates a new date that is based on a date expression and a reference date. This is a very powerful method for date calculation in Business Central.
NewDate := System.CalcDate(DateExpression: String [, Date: Date])

Example:

The message window displays the following text:

Find out more about Using Date Formulas from Microsoft Docs.

8. DMY2Date Method: Gets a Date object based on a day, month, and year.
Date := System.DMY2Date(Day: Integer [, Month: Integer] [, Year: Integer])
Day: The number of the day in the month (1-31)
Month: The number of the month in the year (1-12). If you omit this optional parameter, the current month will be used as the default.
Year: The four-digit number of the year. If you omit this optional parameter, the current year is used as the default.

Example:

9. DWY2Date Method: Gets a Date that is based on a week day, a week, and a year.
Date := System.DWY2Date(WeekDay: Integer [, Week: Integer] [, Year: Integer])
WeekDay: The number of the day in the week (1-7). Monday is day number 1.
Week: The number of the week. Week 1 is the first week of the year that has four or more days in the year. If you omit this optional parameter, the current week is used as the default.
Year: The four-digit number of the year. If you omit this optional parameter, the year of the current week is used as the default.

Example:

The message window displays the following text:

10. DT2Date Method: Gets the date part of a DateTime object.
Date := System.DT2Date(Datetime: DateTime)

Example:

The message window displays the following text:

11. DT2Time Method: Gets the time part of a DateTime object.
Time := System.DT2Time(Datetime: DateTime)

Example:

The message window displays the following text:

OK, Let’s go back to the topic.
Yesterday and Tomorrow:

The message window displays the following text:


First and Last day of the month:

The message window displays the following text:

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

Copied title and URL