Dynamics 365 Business Central: How to easily get Hour, Minute, or Second (HMS) from Time (Time Data Type)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about how to easily get Hour, Minute, Second (HMS) from Time in Business Central.

As you might know, we can use Date2DMY(Date, Integer) Method to get the day, month, or year of a Date Data Type. For example,

Source code:

pageextension 50120 ZYCustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        InputDate: Date;
        Day: Integer;
        Month: Integer;
        Year: Integer;
        Text000: Label 'Today is day %1 of month %2 of the year %3.';
    begin
        InputDate := Today;
        Day := Date2DMY(InputDate, 1);
        Month := Date2DMY(InputDate, 2);
        Year := Date2DMY(InputDate, 3);
        Message(Text000, Day, Month, Year);
    end;
}

But for the Time Data Type, there is no system method to get hour, minute, or second. Is there an easy way to calculate this other than manually? Yes, of cource.

In codeunit 10 “Type Helper”, you can find the standard procedure below, GetHMSFromTime.

So we just need to do the following.

Great.

Very simple, give it a try!!!😁

Source code:

pageextension 50120 ZYCustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        TypeHelper: Codeunit "Type Helper";
        InputTime: Time;
        Hour: Integer;
        Minute: Integer;
        Second: Integer;
        Text000: Label 'It is %1 hours %2 minutes %3 seconds.';
    begin
        InputTime := Time;
        TypeHelper.GetHMSFromTime(Hour, Minute, Second, InputTime);
        Message(Text000, Hour, Minute, Second);
    end;
}

Update 2024.09.05: Business Central 2024 wave 2 (BC25): New Date/Time/DateTime methods

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL