How to format time values for use with the Docentric AX template designer

Introduction

Time values in D365FO are stored as an integer number that represent the number of seconds that have elapsed since midnight (00:00), as explained in documentation here.

Example

For this example, let’s say the current time is 18:42:30 (HH:MM:SS format) and the timezone is CET (UTC+1).
Let’s imagine we have a variable called TimeNow that’s using the FromTime EDT (which is of type EdtTime).
We add this variable to the DDSP with code like this:

FromTime time = timeNow();

_addingRecord.addCalculatedFieldFromEdt('timeNow', extendedTypeStr(FromTime), time, 'Time Now');

The value of this variable in the DDSP will be as follows:
image
So if we do a little adding up, we can see that this value is correct, because 18 h = 64800 s, 42 m = 2520 s, which gives us 64800 + 2520 + 30 = 67350 s.

This is of course extremely hard to parse, so let’s make it better.

How to format time values

To make these values more human readable, we can can take one of these approaches:

Method 1:

Format the EdtTime value into a string by using the time2Str() method:

_addingRecord.addCalculatedField('timeNowFormattedString', time2Str(time, TimeSeparator::Colon, TimeSeparator::Colon), 'Time Now Formatted String');

This will give the following result:
image
Note that the time2str() method automatically adjust the time to fit the time zone.

Method 2:

Convert the EdtTime value into a utcDateTime value and then do the formatting on the Docentric template:

_addingRecord.addCalculatedField('timeNowDateTime', DateTimeUtil::newDateTime(today(), time), 'Time Now Date Time');

Notice that the time zone isn’t automatically taken into account and you can specify exactly which time zone you would like. For example, let’s take the user’s time zone:

_addingRecord.addCalculatedField('timeNowDateTimeTimezone', DateTimeUtil::newDateTime(today(), time, DateTimeUtil::getUserPreferredTimeZone()), 'Time Now Date Time with timezone');

These two snippets of code will produce the following results:
image

Now that we have a datetime value we can format just the time in the Docentric AX designer, like so:

For more information on how to format datetime values, you can check out this forum post.