Hi, Readers.
Today I would like to talk about how to quickly get the day of the week from a date in Business Central.
As you might know, the standard way to get the day of the week from a date in Business Central is to use the System.Date2DWY Method.
System.Date2DWY Method: Gets the day of the week, week number, or year of a Date Data Type.
But as shown above, this method is actually not returning the day of the week, just a number in the week (1=Monday, 2=Tuesday, 3=Wednesday, etc).
For example:
Thursday=4
So, is there a way to directly return the day of the week? Not a number?
Yes, this time we need to use the Date virtual table.
The Date virtual table (ID 2000000007) in Business Central gives you easy access to days, weeks, months, quarters, and years.
The Date virtual table has the following fields.
Field | Description |
---|---|
Period Type | Days, weeks, months, quarters, or years. |
Period Start | The date of the first day in the period. |
Period End | The date of the last day in the period. |
Period No. | The number of the period. |
Period Name | The name of the period. |
And the primary key is “Period Type”, “Period Start”.
You can force Business Central to run the “Date” table by adding the ?table=2000000007 parameter to the URL, such as in the following example:
https://businesscentral.dynamics.com/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox?table=2000000007
The dates from 01/03/0001 to 12/31/9999 will be displayed by different period types.
Let’s see some details.
If Period Type is Date
Period No.: The day of the week (1=Monday, 2=Tuesday, 3=Wednesday, etc)
Period Name: Name of the date (Monday, Tuesday, Wednesday, etc)
If Period Type is Week
Period No.: Week Number of the year
Period Name: Week Number of the year
If Period Type is Month
Period No.: Month Number (1=Jaunary, 2=Frbruary, 3=March, etc)
Period Name: Name of the month
If Period Type is Quarter
Period No.: Quarter Number (1=Jaunary~March, 2=April~June, 3= July~September, etc)
Period Name: Quarter Number (1=Jaunary~March, 2=April~June, 3= July~September, etc)
If Period Type is Year
Period No.: Year
Period Name: Year
Okay, I believe that you should already know how to use it.
let’s try again.
PS: The day of the week got in this way will be automatically translated.
Test Video:
Source Code:
pageextension 50112 ZYCustomerListExt extends "Customer List"
{
trigger OnOpenPage()
var
Msg: Label 'Today is %1.\And today is %2.';
DateForWeek: Record Date;
begin
if DateForWeek.Get(DateForWeek."Period Type"::Date, Today) then
Message(Msg, Today, DateForWeek."Period Name");
end;
}
PS:
There is another easy way, using System.Format Method.
For example:
The result:
Source Code:
pageextension 50112 ZYCustomerListExt extends "Customer List"
{
trigger OnOpenPage()
var
Msg: Label 'Today is %1.\And today is %2.';
begin
Message(Msg, Today, Format(Today, 0, '<Weekday Text>'));
end;
}
You can find more about Formatting Values, Dates, and Time from Microsoft Docs.
END
Hope this will help.
Thanks for reading.
ZHU
コメント