Converting UTC DateTime to ISO 8601 in D365FO

Converting UTC DateTime to ISO 8601 in D365FO

Hello D365 Champs ! Recently, I had the opportunity to write a code snippet to convert a UTC DateTime value into ISO 8601 format. This format is widely used for representing dates and times in a standardized way.

In D365FO, date and time handling is crucial for consistency across systems, especially when dealing with integrations. ISO 8601 is a standardized format for date-time representation.

Below is the code I came up with, and you can modify it to suit your specific requirements.

public static str GetISOTime(utcdatetime _DateTimeValue)

{

// Variables for date and time parts

int year, month, day, hour, minute, second;

str formattedDateTime;

System.DateTime localTime;

// Extract components of the given UTC DateTime

year = DateTimeUtil::year(_DateTimeValue);

month = DateTimeUtil::month(_DateTimeValue);

day = DateTimeUtil::day(_DateTimeValue);

hour = DateTimeUtil::hour(_DateTimeValue);

minute = DateTimeUtil::minute(_DateTimeValue);

second = DateTimeUtil::second(_DateTimeValue);

// Create a .NET DateTime object with the extracted components

localTime = new System.DateTime(year, month, day, hour, minute, second, 0, System.DateTimeKind::Utc);

// Convert UTC to "Arabic Standard Time"

localTime = System.TimeZoneInfo::ConvertTime(localTime, System.TimeZoneInfo::FindSystemTimeZoneById("Arabic Standard Time"));

// Format as ISO 8601 with offset (+03:00 for Arabic Standard Time)

formattedDateTime = localTime.ToString("o");

// Adjust milliseconds formatting (optional, based on requirement)

formattedDateTime = strReplace(formattedDateTime, ".0000000", ".00+03:00");

return formattedDateTime;

}

Example Usage

utcNow = DateTimeUtil::utcNow();

str iso8601DateTime = YourClassName::GetEISOTime(utcNow);

info(strFmt("ISO 8601 DateTime: %1", iso8601DateTime));

Example Output

For a given UTC time of 2025-01-27T11:23:45Z, the function will return

2025-01-27T14:23:45.00+03:00


要查看或添加评论,请登录

Muhammad Umer的更多文章

社区洞察

其他会员也浏览了