Please wait
Talk to Expert
Microsoft Dynamics Business central

New Date/Time/DateTime Methods Business Central 2024 wave 2 (BC25)

10

Introduction

In the latest update of Microsoft Dynamics 365 Business Central (BC25) in Wave 2 of 2024, there are some significant improvements and additions to how the system handles date, time, and datetime values. Developers can now leverage new methods for better handling time-based data, providing more flexibility and accuracy when working with time and date fields.

This blog walks through the new methods introduced for Date, Time, and DateTime data types in BC25. We’ll also provide practical code examples to help you implement these methods in your Business Central extensions and applications.

What’s New in Date/Time/DateTime Methods in BC25?

With BC25, Microsoft has added several new methods that provide better functionality for working with date and time datatypes. These changes aim to simplify and enhance developers’ manipulation of time and date values in AL code.

Date DataType

Why the New Date Data Type?

Before Business Central 25, date handling in AL code could be somewhat cumbersome, with developers needing to use text-based or less specialized fields to manipulate dates. The new Date data type solves this problem by providing a dedicated structure for working with date values, which helps prevent errors, ensures better localization, and improves the overall efficiency of your code.

Code Example: Working with the Date Data Type

Let’s take a look at a concrete example to see how you can use the Date data type in a real-world scenario. In this example, we are going to create a table that stores an Input Date Value (of type Date) and uses it to extract various components of the date, such as the Day, Month, Year, Day of the Week, and Week Number.



How the Code Works:

Table Definition: In the code above, we define a table (50100 “New Date/Time/DateTime Methods”) that contains several fields for storing different components of a date.

The Input Date Value field is of type Date and will be used to store the actual date input by the user.

We also have fields for storing the extracted components, including Day, Month, Year, Day of the Week, and Week Number.

OnValidate Trigger: When a value is entered into the Input Date Value field, the OnValidate trigger fires, which calls the Find Day\Month\Year\DayOfWeek\WeekNo From Date procedure to extract and populate the components of the date.

Extracting Date Components:

Using the Date data type, we can easily extract the day (Day), month (Month), year (Year), day of the week (DayOfWeek), and week number (WeekNo) directly from the Input Date Value.

Time DataType

What’s New with the Time Data Type?

The introduction of the Time data type in Business Central 25 provides a structured, standardized way to store and manipulate time. In previous versions, developers often had to rely on text fields or complex custom logic to work with time components, such as hours, minutes, seconds, and milliseconds. The Time data type offers a more efficient, error-free way to handle these operations.

Code Example: Working with the Time Data Type

Let’s explore a practical example of how you can use the Time data type in Business Central 25. In this example, we will create a table that stores an Input Time Value (of type Time) and extracts the components of the time, such as the Hour, Minute, Second, and Millisecond.



How the Code Works:

Table Definition: The table 50100 “New Date/Time/DateTime Methods” includes an Input Time Value field of type Time. This field will store the time input by the user, and the associated components will be extracted automatically when the time value is validated.

OnValidate Trigger: The OnValidate trigger is attached to the Input Time Value Whenever the value in this field is updated or validated, the Find Hour\Minute\Second\Millisecond procedure is called.

Extracting Time Components: – The procedure Find Hour\Minute\Second\Millisecond uses the built-in properties of the Time data type to extract individual time components:

Hour: Extract the hour part of the time value.
Minute: Extract the minute part.
Second: Extract the second part.
Millisecond: Extract the millisecond part (if applicable)

DateTime DataType

What’s New with the DateTime Data Type?

In Business Central 25, the new DateTime data type provides a more efficient and flexible way to handle date and time information. By allowing developers to easily extract Date and Time components from a single DateTime field, it simplifies the code and improves data integrity, making it easier to work with DateTime data in Business Central applications.

Code Example: Extracting Date and Time from a DateTime Field

Let’s see how the new DateTime data type works in practice. In this example, we’ll demonstrate how to extract the individual Date and Time components from a DateTime field. We’ll use a table that stores a DateTime value in a field and then separate the date and time into their respective fields.




If you’re curious about how we can assist you in reaching your goals, don’t hesitate to get in touch. Our dedicated team is ready to provide support every step of the way.

Let’s turn your vision into reality together!