Date and time format strings in Power Automate

In a recent article, we investigated how can we work with dates and time within Power Automate, exploring methods to capturing the current time, adding or subtracting intervals, calculating the difference between dates, and more. While Power Automate represents date and time as timestamps, a format highly effective for data management, it may not be the most user-friendly option for all users.

In this article, we will explore how to format date and time in Power Automate by using the format string, which offers a high level of customization to the user.

Timestamp in Power Automate

In Power Automate, a timestamp represents a precise moment in time, including the date, time, and sometimes the time zone. Timestamps can be extracted from actions outputs like the moment when a file was created, or generated by an expression, such as utcNow().

Timestamps are important for tracking events, scheduling tasks or recording data, being very useful in automating tasks that require time-sensitive actions, such as sending notifications, updating records, or triggering events based on specific temporal conditions. As the timestamps follow the ISO 8601 format, it can be easily processed and synchronized across various systems and applications.

This is how a timestamp looks like in Power Automate. The value was generated using the utcNow() expression:

timestamp variable in power automate

In the image above, all text preceding the “T” in a timestamp represents the date (highlighted in yellow), formatted as “year-month-day”. Following the “T”, we observe the time representation (highlighted in green), in hours, minutes, seconds, and milliseconds. In some cases, it is also possible to have a timezone information, which will come after the “Z”, at the end of the timestamp.

Strategies to format timestamps in Power Automate

Timestamps can be very useful to represent the precise moment when an event occurred, but its default format is not user-friendly. To address this issue, Power Automate offers the fromatDateTime() expression, which allows the users to customize the presentation of timestamps according to their preferences. In addition, many regular date/time expressions allow an optional “format” parameter (highlighted in yellow):

format parameter in power automate expressions

This optional format parameter expects a string (for further reference on data types in Power Automate, refer to this article), that can be either a standard or custom format string. Standard format strings represents the timestamp in some of the most commonly used date/time representation formats (like “April 22” or 4/22/2024 7:52:00 AM), while the Custom format strings offer a high level of customization (like “April, 22 – 2024, 07:52” or “2024 Apr 22”).

Standard format strings in Power Automate

The Standard format string takes a single character to generate the formatted timestamp. In the image below, we are taking the dynamic content of the “timestamp” variable (highlighted in yellow) and formatting it as “d” (highlighted in green), which represents a short date:

standard format string in power automate

As a result, we get this date format:

short date in power automate

If we use the same expression, but replace “d” for “U”, the result will be a totally different format, which will also include the time:

long date in power automate

You can find a complete list of the standard format strings in the Microsoft official documentation by clicking here.

Custom format strings in Power Automate

In addition to the standard ones, custom format strings offer the user the flexibility to adapt the timestamp to specific requirements. These custom format strings work with restricted characters for each time interval, such as “y” for year, “d” for day and “s” for second. The number of times a character is repeated in the string determines its representation: for example, “dddd” represents the full name of the day of week (“Monday”), “ddd” the abbreviated name of the day of week (“Mon”), and “d” the day of the month. Similar rules apply for the other intervals, like month and hour. You can find the official list of available intervals here.

Custom format strings offer the flexibility to include special characters such as commas or dashes, and users can arrange the order of intervals as desired, even repeating some. In the example below, we repeat the “day” interval, presenting it in two different formats:

custom format string in power automate

This is the output:

custom date in power automate

While the custom format strings offer greater flexibility than the standard ones, they still have limitations. Any character that coincides with the intervals in the custom format strings, such as “d”, “y”, or “m”, will automatically be interpreted as these intervals. Consequently, a format like this won’t work as expected:

custom format string with words in power automate

The “y” in the word “year” will be replaced by the year number, and the same will occur with all other reserved characters included in the string. This is the output:

incorrect date output in power automate

The solution here is to work with separated formatDateTime() expressions, one for each interval, and then combining them with a concat() expression. To learn more about how to work with tricky text expressions, check this article.

Conclusion

Timestamps are a great feature from Power Automate, as they have the capacity to easily represent a precise moment int time. In most cases, however, the interval will need to be presented in a more user-friendly format. As a solution, Power Automate has the format strings, which offers an alternative to overcome challenges posed by complex formatting requirements, enabling users to craft timestamps that meet their exact specifications.

By Raphael Zaneti

Power Platform Developer