Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Business Central forum

Regarding time field

(0) ShareShare
ReportReport
Posted on by 700

Hi All, 

Is it possible to convert the value in time field to hours format, for comparison purpose?

For example, the user enters start time as x AM and end time as y AM/PM. Now, the difference between both should not be more than 12 hours. How to put this validation in AL code??

Please respond. 

Thanks! 

Categories:
  • Dynamics 365 Business Central Profile Picture
    700 on at
    RE: Regarding time field

    When I am entering 8 pm to 12 am, it is calculating a duration of -8 hours, thus not accepting

  • Avinash B Profile Picture
    1,455 on at
    RE: Regarding time field

    Then have 2 date fields, Start Date and End Date. when they enter end time greater than 12, you must change the end date to next date.

  • Dynamics 365 Business Central Profile Picture
    700 on at
    RE: Regarding time field

    Actually we cannot change the requirement of the project. Is there any solution available ?

  • Avinash B Profile Picture
    1,455 on at
    RE: Regarding time field

    Hi

    I feel in your case 10 AM means the next day.  So I would suggest to use Duration and let the user enter it and calculate the end time automatically.

    Regards

    Avinash B

  • Dynamics 365 Business Central Profile Picture
    700 on at
    RE: Regarding time field

    Now, 11 PM to 10 AM is also throwing error, but it should not

  • Suggested answer
    Avinash B Profile Picture
    1,455 on at
    RE: Regarding time field

    try the below code on validate of the End time field.

    If "End Time" < "Start Time" then

    error('End time can''t be greater than Start time');

  • Dynamics 365 Business Central Profile Picture
    700 on at
    RE: Regarding time field

    Thank You so much, Avinash. Need to ask about one more validation.

    Such as, if start time is 11 PM (night) and end time is 12 PM (afternoon), then too system should show error. What should be the logic for this one ?

  • Suggested answer
    Avinash B Profile Picture
    1,455 on at
    RE: Regarding time field

    Hi

    try this.

    table 50117 "Standard Office Schedule"

    {

      DataClassification = ToBeClassified;

      fields

      {

          field(1; "Code"; Code[50])

          {

              DataClassification = ToBeClassified;

          }

          field(2; "Scheduled Start Time"; Time)

          {

              DataClassification = ToBeClassified;

          }

          field(3; "Scheduled End Time"; Time)

          {

              DataClassification = ToBeClassified;

              trigger OnValidate()

              var

                  Duration: duration;

                  Hours: decimal;

              begin

                 if "Scheduled End Time" > 0T then begin

                  Duration := "Scheduled End Time" - "Scheduled Start Time";

                  Hours := ((Duration / 1000) / 60) / 60;

                  if (Hours > 12) then begin

                      Error('Duration cannot be more than 12 hours');

                 end;

                  end;

              end;

          }

          field(4; "Standard Lunch Break"; Integer)

          {

              DataClassification = ToBeClassified;

          }

      }

      keys

      {

          key(PK; "Code")

          {

              Clustered = true;

          }

      }

      var

          myInt: Integer;

          Hours: Decimal;

      trigger OnInsert()

      begin

      end;

      trigger OnModify()

      begin

      end;

      trigger OnDelete()

      begin

      end;

      trigger OnRename()

      begin

      end;

    }

    Regards

    Avinash B

  • Dynamics 365 Business Central Profile Picture
    700 on at
    RE: Regarding time field

    Please check if the below code is correct or not, to achieve the purpose !

    table 50117 "Standard Office Schedule"

    {

       DataClassification = ToBeClassified;

       fields

       {

           field(1; "Code"; Code[50])

           {

               DataClassification = ToBeClassified;

           }

           field(2; "Scheduled Start Time"; Time)

           {

               DataClassification = ToBeClassified;

           }

           field(3; "Scheduled End Time"; Time)

           {

               DataClassification = ToBeClassified;

               trigger OnValidate()

               var

                   myInt: Integer;

               begin

                   if (Hours > 12) then begin

                       Error('Duration cannot be more than 12 hours');

                   end;

               end;

           }

           field(4; "Standard Lunch Break"; Integer)

           {

               DataClassification = ToBeClassified;

           }

           field(5; "Duration"; Duration)

           {

               DataClassification = ToBeClassified;

               trigger OnValidate()

               var

                   myInt: Integer;

               begin

                   Duration := "Scheduled End Time" - "Scheduled Start Time";

                   Hours := ((Duration / 1000) / 60) / 60;

               end;

           }

       }

       keys

       {

           key(PK; "Code")

           {

               Clustered = true;

           }

       }

       var

           myInt: Integer;

           Hours: Decimal;

       trigger OnInsert()

       begin

       end;

       trigger OnModify()

       begin

       end;

       trigger OnDelete()

       begin

       end;

       trigger OnRename()

       begin

       end;

    }

  • Suggested answer
    Avinash B Profile Picture
    1,455 on at
    RE: Regarding time field

    Hi

    Just check this out

    forum.mibuso.com/.../calculation-of-time-difference

    Regards

    Avinash B

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans