Hi, Readers.
Today I would like to share another mini tip about Business Central, how to convert Integer/Text to Date.
When developing in AL, handling data type conversions—specifically moving between Text, Integers, and Dates—is a fundamental skill. Whether you are processing API responses or building custom import logic, here is a quick guide on the most effective ways to handle these conversions.
Converting Text to Date
The most common scenario is parsing a string into a valid Date variable.
The Standard Way: Evaluate
System.Evaluate(var Any, Text [, Integer]) Method: Evaluates a string representation of a value into its typical representation. The result is assigned to a variable.
It uses the user’s regional settings to parse the string. Always wrap it in an if statement to handle formatting errors gracefully.
For example,
var
DateText: Text;
TargetDate: Date;
begin
DateText := '2026-02-20';
if Evaluate(TargetDate, DateText) then
// Logic for success
else
Error('Invalid date format: %1', DateText);
end;Let’s look at a specific example.
Test video:
Test code:
pageextension 50127 SalesOrderExt extends "Sales Order"
{
layout
{
addafter("Sell-to Customer Name")
{
field(DateText; DateText)
{
ApplicationArea = All;
Caption = 'Date Text';
trigger OnValidate()
var
TargetDate: Date;
begin
if DateText = '' then
exit;
if Evaluate(TargetDate, DateText) then
Rec."Posting Date" := TargetDate
else
Error('Invalid date format: %1', DateText);
end;
}
}
}
var
DateText: Text;
}The Precision Way: String Slicing + DMY2Date
System.DMY2Date(Integer [, Integer] [, Integer]) Method: Gets a Date object based on a day, month, and year.
If you receive a fixed format (like YYYYMMDD) that doesn’t match the user’s locale, use CopyStr to break it down:
// Example: Input '20260220'
Evaluate(Y, CopyStr(RawText, 1, 4));
Evaluate(M, CopyStr(RawText, 5, 2));
Evaluate(D, CopyStr(RawText, 7, 2));
TargetDate := DMY2Date(D, M, Y);Applying the logic from the earlier example, we can adjust it as follows:
Test code:
pageextension 50127 SalesOrderExt extends "Sales Order"
{
layout
{
addafter("Sell-to Customer Name")
{
field(DateText; DateText)
{
ApplicationArea = All;
Caption = 'Date Text';
trigger OnValidate()
var
TargetDate: Date;
D: Integer;
M: Integer;
Y: Integer;
begin
if DateText = '' then
exit;
Evaluate(Y, CopyStr(DateText, 1, 4));
Evaluate(M, CopyStr(DateText, 5, 2));
Evaluate(D, CopyStr(DateText, 7, 2));
TargetDate := DMY2Date(D, M, Y);
Rec."Posting Date" := TargetDate;
end;
}
}
}
var
DateText: Text;
}PS: There are two other special treatments we’ve discussed before.
- How to get the Month Name/Text from a date (Two ways)
- How to convert Month Name (Text) to Month Number (Integer)
Converting Integer to Date
Converting an integer to a date in Business Central (AL) isn’t a direct “type conversion” because an integer doesn’t inherently know which day, month, or year it represents. Depending on what your integer actually stands for (e.g., a date integer, or just separate Day/Month/Year values), you have two primary ways to handle this.
The DMY2Date Method (Most Common)
System.DMY2Date(Integer [, Integer] [, Integer]) Method: Gets a Date object based on a day, month, and year.
For example,
trigger OnRun()
var
myDay: Integer;
myMonth: Integer;
myYear: Integer;
resultingDate: Date;
begin
myDay := 15;
myMonth := 3;
myYear := 2026;
// Converts integers to March 15, 2026
resultingDate := DMY2Date(myDay, myMonth, myYear);
end;This was discussed in detail when we talked about Select the only Year (Month or Day) when entering dates(Only year drop).
Converting “Serialized” Integers (e.g., 20260315)
If your integer is in a format like YYYYMMDD (e.g., 20260315), you must first parse it into strings or use math to break it apart before converting.
Parse it into strings: This is similar to the String Slicing processing mentioned above.
// Example: Input '20260220'
Evaluate(Y, CopyStr(Format(DateInteger), 1, 4));
Evaluate(M, CopyStr(Format(DateInteger), 5, 2));
Evaluate(D, CopyStr(Format(DateInteger), 7, 2));
TargetDate := DMY2Date(D, M, Y);Use math to break it: personally do not recommend
var
inputInt: Integer; // 20260315
Y: Integer;
M: Integer;
D: Integer;
FinalDate: Date;
begin
inputInt := 20260315;
Y := inputInt div 10000; // 2026
M := (inputInt div 100) mod 100; // 03
D := inputInt mod 100; // 15
FinalDate := DMY2Date(D, M, Y);
end;Give it a try!!!😁
PS:
1. New Date/Time/DateTime methods
2. Convert simple type values to text using new ToText method (without using System.Format Method)
END
Hope this will help.
Thanks for reading.
ZHU
コメント