AX / D365F – Getting current date in a specific format (dd/mm/yyyy) using DateTimeUtil::toFormattedStr – X++

The DateTimeUtil::toFormattedStr method in X++ is useful for formatting dates according to specific patterns. However, to achieve a specific format like dd/mm/yyyy, you need to use the appropriate date sequence code. In AX 2012 and Dynamics 365 for Finance and Operations, DateTimeUtil::toFormattedStr may not directly support a custom format string like dd/mm/yyyy. Instead, you’ll typically use other utility functions or string formatting methods to achieve this format.

Here’s how you can achieve the dd/mm/yyyy format using DateTimeUtil::toFormattedStr and a combination of other X++ methods:

static void FormatDateTimeJob(Args _args)
{
    utcDateTime now = DateTimeUtil::utcNow();
    str formattedOutput;

    formattedOutput = DateTimeUtil::toFormattedStr(
        now, 
        123, //dd/mm/yyyy
        DateDay::Digits2, 
        DateSeparator::Slash, 
        DateMonth::Digits2, 
        DateSeparator::Slash, 
        DateYear::Digits4, 
        TimeSeparator::Colon, 
        TimeSeparator::Colon);
    info(formattedOutput);
}

Leave a comment