Dynamics 365 Business Central: Working with Duration Data Type

Dynamics 365 Business Central

Hi, Readers.
Recently, I was asked how to change the output format of Duration data type, how to convert the unit to hours or minutes, etc. So, today I would like to briefly discuss Duration data type in Business Central.

Duration Data Type:

Represents the difference between two DateTimes. This value can be negative. It is stored as a 64-bit integer. The integer value is the number of milliseconds during the duration.

The following are examples of durations:

DateTime – DateTime = Duration

DateTime – Duration = DateTime

DateTime + Duration = DateTime

For example: Shows how to calculate the difference between two DateTimes.

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        DateTime1: Datetime;
        DateTime2: Datetime;
        Duration: Duration;

    begin
        DateTime1 := CreateDateTime(20210101D, 000000T);
        DateTime2 := CurrentDateTime;
        Duration := DateTime2 - DateTime1;
        Message(Format(Duration));
    end;
}

The message window displays the following:
39 days 16 hours 45 minutes 38 seconds 807 milliseconds

PS: If milliseconds are not included in the calculation, milliseconds will be omitted.
40 days 9 hours 42 minutes 35 seconds

1. Convert the unit to hours or minutes

A duration type field is really just a special representation of a BigInteger field, if you look closely at the duration type field you’ll find that it only contains the number of milliseconds.

Let’s look at an example.
Calculate the duration between 2021/01/01 00:00:00 ~ 2021/02/10 09:42:35. (Only accurate to the second)

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        DateTime1: Datetime;
        DateTime2: Datetime;
        Duration: Duration;
        InputString: Text[1024];
        StringList: List of [Text];
        OutputString: Label 'The value of Duration is %1.';


    begin
        DateTime1 := CreateDateTime(20210101D, 000000T);
        DateTime2 := CreateDateTime(20210210D, 094235T);
        Duration := DateTime2 - DateTime1;
        Message(Format(Duration));

        Message(OutputString, Format(Duration / 1));

    end;
}

The first message: 40 days 9 hours 42 minutes 35 seconds

The second message: The value of Duration is 3,490,955,000.

So it’s very easy to calculate each unit:

milliseconds = Duration / 1

seconds = Duration / 1000

minutes = Duration / 60000

hours = Duration / 3600000

days = Duration / 86400000

For example: (You can use ROUND or Div to get the clean result you want.)
Only need hours.
The value of hours is 969.71.

Only need minutes.
The value of minutes is 58,182.

2. Change the format

For example: Display 09:42 instead of ‘9 hours 42 minutes’.
In Business Central forum and community, there are some solutions, similar to the method described above, the value of each item can be obtained by calculation.
This time I want to share another simple method. Use List Data Type and Text.Split Method.
Text.Split Method: Splits a string into a maximum number of substrings based on a collection of separators.
List Data Type: Represents a strongly typed list of ordered objects that can be accessed by index.
Next, let us look at an example.

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        DateTime1: Datetime;
        DateTime2: Datetime;
        Duration: Duration;
        InputString: Text[1024];
        StringList: List of [Text];
        OutputString: Label 'Day is %1\\Hour is %2\\Minute is %3\\Second is %4';

    begin
        DateTime1 := CreateDateTime(20210101D, 000000T);
        DateTime2 := CreateDateTime(20210210D, 094235T);
        Duration := DateTime2 - DateTime1;
        Message(Format(Duration));
        InputString := Format(Duration);
        StringList := InputString.Split(' ');
        Message(OutputString, StringList.Get(1), StringList.Get(3), StringList.Get(5), StringList.Get(7));
    end;
}

So, for the problem, display 9:42 instead of ‘9 hours 42 minutes’. You can do it very easily.

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

Copied title and URL