0

In X++, str2Date is a runtime function that converts the specified string to a date value. It is typically used when you have a date expressed as text, such as when reading from ActiveDate.valueStr() and storing this into a string variable. The str2Date function takes two parameters: the input string and a three-digit sequence number that specifies the order of the day, month, and year. For example, 123 represents a day/month/year format, while 213 indicates month/day/year. If the input string matches the sequence, a valid date is returned; if it does not, a zero date is returned.

The syntax is straightforward:

date str2Date(str text, int sequence);

Here’s a simple demonstration:

static void Demo_str2Date(Args _args)
{
    str s = "16/06/2015";
    date dateFrom = str2Date(s, 123);
    info(strFmt("Converted date: %1", dateFrom));
}

In this example, the output is a correctly converted date value when the sequence matches the string format. If the parameters are mismatched or invalid, the function does not throw an error but instead returns a default zero date. The following two examples specify an invalid date:

dateFrom = str2Date("31/02/2015", 123); // February 31 is invalid
dateFrom = str2Date("13/13/2015", 123); // Month 13 does not exist

Because the return type is a date and not utcdatetime, the function is best used for operations that don’t require a time component. As it’s easy to spot, the methods are equivalents for the AsOfDate and AsOfDateRange modes from X++ select statements, allowing developers to tie the string-based inputs directly to query conditions. For example:

dateFrom = str2Date("16/06/2015", 123); 
// Equivalent behavior when used with AsOfDate or AsOfDateRange in select statements

In summary, str2Date gives developers a reliable way to normalize text into proper date values, ensuring consistent comparisons, storage, and filtering. It complements other runtime functions like str2datetime and date2str, making date handling in AX / D365FO both flexible and predictable.

Have a Question ?

Fill out this short form, one of our Experts will contact you soon.

Call Us Today For Your Free Consultation