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:
![](https://yzhums.com/wp-content/uploads/2020/12/image-18.png)
On the regional format set to English (United States), the message window could display the following:
![](https://yzhums.com/wp-content/uploads/2020/12/image-19.png)
2. Time Method: Gets the current time from the operating system.
Example:
![](https://yzhums.com/wp-content/uploads/2020/12/image-21.png)
On the regional format set to English (United States), the message window could display the following:
![](https://yzhums.com/wp-content/uploads/2020/12/image-20.png)
3. WorkDate Method: Gets and sets the work date for the current session.
![](https://yzhums.com/wp-content/uploads/2020/12/image-37.png)
Example:
![](https://yzhums.com/wp-content/uploads/2020/12/image-23.png)
On the regional format set to English (United States), the message window could display the following:
![](https://yzhums.com/wp-content/uploads/2020/12/image-22.png)
4. CurrentDateTime Method: Gets the current DateTime.
Example:
![](https://yzhums.com/wp-content/uploads/2020/12/image-25.png)
On the regional format set to English (United States), the message window could display the following:
![](https://yzhums.com/wp-content/uploads/2020/12/image-24.png)
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:
![](https://yzhums.com/wp-content/uploads/2020/12/image-27.png)
The message window displays the following:
![](https://yzhums.com/wp-content/uploads/2020/12/image-26.png)
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:
![](https://yzhums.com/wp-content/uploads/2020/12/image-29.png)
The message window displays the following information:
![](https://yzhums.com/wp-content/uploads/2020/12/image-28.png)
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])
![](https://yzhums.com/wp-content/uploads/2020/12/image-30.png)
Example:
![](https://yzhums.com/wp-content/uploads/2020/12/image-32.png)
The message window displays the following text:
![](https://yzhums.com/wp-content/uploads/2020/12/image-31.png)
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:
![](https://yzhums.com/wp-content/uploads/2020/12/image-39.png)
![](https://yzhums.com/wp-content/uploads/2020/12/image-38.png)
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:
![](https://yzhums.com/wp-content/uploads/2020/12/image-41.png)
The message window displays the following text:
![](https://yzhums.com/wp-content/uploads/2020/12/image-40.png)
10. DT2Date Method: Gets the date part of a DateTime object.
Date := System.DT2Date(Datetime: DateTime)
Example:
![](https://yzhums.com/wp-content/uploads/2020/12/image-43.png)
The message window displays the following text:
![](https://yzhums.com/wp-content/uploads/2020/12/image-42.png)
11. DT2Time Method: Gets the time part of a DateTime object.
Time := System.DT2Time(Datetime: DateTime)
Example:
![](https://yzhums.com/wp-content/uploads/2020/12/image-45.png)
The message window displays the following text:
![](https://yzhums.com/wp-content/uploads/2020/12/image-44.png)
OK, Let’s go back to the topic.
Yesterday and Tomorrow:
![](https://yzhums.com/wp-content/uploads/2020/12/image-34.png)
The message window displays the following text:
![](https://yzhums.com/wp-content/uploads/2020/12/image-33.png)
First and Last day of the month:
![](https://yzhums.com/wp-content/uploads/2020/12/image-36.png)
The message window displays the following text:
![](https://yzhums.com/wp-content/uploads/2020/12/image-35.png)
END
Hope this will help.
Thanks for your reading.
ZHU
コメント