Hi, Readers.
Today I would like to share another tip about Business Central. During the development, there may be situations where you need to get the Start Date and End Date of the month from a given date. How can we do this quickly?
This is not very difficult.
This time we use CalcDate Method.
CalcDate Method: Calculates a new date that is based on a date expression and a reference date.
Syntax: NewDate := System.CalcDate(DateExpression: String [, Date: Date])
Parameters:
DateExpression
Type: String
The date expression can be any length. The string is interpreted from left to right with one subexpression at a time. The following rules describe the valid syntax of date expressions:
- DateExpression = [<Subexpression>][<Subexpression>][<Subexpression>]
- <Subexpression> = [<Sign>] <Term>- <Sign> = + | –
- <Term> = <Number><Unit> | <Unit><Number> | <Prefix><Unit>
- <Number> = Positive integer
- <Unit> = D | WD | W | M | Q | Y (D=day, WD=weekday, W=week, M=month, Q=quarter, Y=year)
- <Prefix> = C (C=current) These production rules show that date expressions consist of zero, one, two, or three subexpressions. Each subexpression consists of an optional sign and a term. The following are some typical examples of terms:
- 30D (30 days; corresponds to <Number><Unit>)
- WD2 (weekday number 2; corresponds to <Unit><Number>)
- CW (current week; corresponds to <Prefix><Unit>) The internal calendar starts on Monday and ends on Sunday. This means that Monday is weekday 1 and Sunday is weekday 7. A run-time error occurs if the syntax of DateExpression is incorrect.
[Optional] 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.
Next, let’s see how to do it.
It actually takes only two lines of code to do this.
StartDate := CalcDate('<-CM>', Givendate);
EndDate := CalcDate('<CM>', Givendate);
For example:
Source Code:
pageextension 50100 MyExtension extends "Customer List"
{
trigger OnOpenPage()
var
StartDate: Date;
EndDate: Date;
SalesOrderSubform: Page "Sales Order Subform";
Msg: Label 'Today is ''%1''\Start Date of the month is ''%2''\End Date of the month is ''%3''';
begin
StartDate := CalcDate('<-CM>', Today);
EndDate := CalcDate('<CM>', Today);
Message(Msg, Today, StartDate, EndDate);
end;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント