Header Ads Widget

How to Format Date in D365FO Using X++ | Convert Date to Custom String Format

 

📝 Introduction

In Dynamics 365 Finance and Operations (D365FO), there are scenarios where you might need to display a date in a custom format — for reports, logs, filenames, or user messages.

X++ provides a powerful utility function, date2str(), which allows you to convert a date type to a formatted string using various layout options.

This post explains how to format a date in D365FO using modern and clear X++ code.


🧩 Example: Convert Today’s Date to Custom String Format

x++
static void D365FO_CustomDateFormatExample(Args _args) { // Declare today's date date currentDate = today(); str formattedDate; // Convert date to string with custom format formattedDate = date2str( currentDate, 123, // Sequence of formatting DateDay::Digits2, // Day as 2-digit (e.g., 01, 25) DateSeparator::Hyphen, // Separator after day (-) DateMonth::Short, // Month as short name (e.g., Jan, Feb) DateSeparator::Hyphen, // Separator after month (-) DateYear::Digits4 // Year as 4-digit (e.g., 2025) ); info(strFmt("Today's formatted date is: %1", formattedDate)); }

🔎 Format Explanation:

ArgumentValuePurpose
DateDay::Digits22-digit day (e.g., 01)Format day component
DateSeparator::Hyphen-Separator between date parts
DateMonth::ShortShort month name (e.g., Apr)Format month as short text
DateYear::Digits44-digit year (e.g., 2025)Format year component

You can customize it easily using:

  • DateSeparator::Slash instead of Hyphen

  • DateMonth::Digits2 for numeric month

  • DateDay::Name to include weekday


🔄 Alternate Formats

Example 1: MM/DD/YYYY Format

x++
formattedDate = date2str( currentDate, 123, DateMonth::Digits2, DateSeparator::Slash, DateDay::Digits2, DateSeparator::Slash, DateYear::Digits4);

Example 2: Full Date with Day Name

x++
formattedDate = date2str( currentDate, 321, DateDay::Name, DateSeparator::Space, DateMonth::Name, DateSeparator::Space, DateYear::Digits4);

✅ Summary

  • Use date2str() to convert and format dates in D365FO.

  • Customize separators, order (day/month/year), and format style.

  • Helps in consistent date presentation across reports, emails, logs, etc.


🔖 Tags

D365FO Date Format, X++ date to string, How to use date2str(), Convert date in D365, X++ formatting examples, D365FO reports, X++ today date, Custom date separator X++, AX to D365FO Date Format

Post a Comment

0 Comments