How to Format Dates and Times in Power Automate: A Guide to Functions and Time Zones

Power Automate allows you to transform date data into various display formats. Here’s a summary of how to achieve this effectively.

スポンサーリンク

Date Time Zones and Formatting

To begin, let’s briefly explain the concepts of date and time display formats and time zones.

Time Zones

When working with dates and times, it’s essential to consider time zones if you need to account for time differences.

Coordinated Universal Time (UTC) serves as the global standard, with each country adjusting its time based on UTC. Here’s an example:

  • Coordinated Universal Time (UTC): The global standard time
  • Japan: UTC +9 hours
  • US Eastern Time: UTC -5 hours
  • US Pacific Time: UTC -7 hours
  • Central European Time: UTC +1 hour

*Note: Daylight Saving Time is not considered here.

The utcNow function in Power Automate returns the current time in UTC. To obtain the current time in Japan, you can either “specify the time zone” or “add 9 hours to the utcNow function” result.

When specifying a time zone, use the values (strings) provided on the following site:
Microsoft Time Zone Index Values

Date and Time Formatting

Formatting is used to specify how you want to display dates and times, such as “2021-01-27” or “27/01/2021 13:45:00” depending on your desired format.

There are “predefined formats” already available, but if none of them meet your needs, you can create a custom format.

When using predefined formats to convert “2009-06-15T13:45:30,” the results are as follows:

  • Short date format: 6/15/2009
  • Full date in short format: Monday, June 15, 2009 1:45 PM
  • General date in long format: 6/15/2009 1:45:30 PM
  • RFC1123 pattern: Mon, 15 Jun 2009 13:45:30 GMT
When specifying a custom format to convert “2009-06-15T13:45:30,” the results are as follows:

  • ‘yyyyMMdd’: 20090615
  • ‘yyyy-MM-dd-HH:mm:ss’: 2009-06-15-13:45:30
  • ‘yyMMdd(ddd)’: 090615(Mon)

Functions for Specifying Date and Time Formats and Time Zones

Now, let’s dive into the main topic: introducing functions that allow you to convert date and time formats or change time zones.

Convert from Coordinated Universal Time (UTC) to a Specified Time Zone: convertFromUtc

 convertFromUtc('timestamp', 'destinationTimeZone', 'format'?) 

This function converts the time specified in the first argument as UTC to the time zone specified in the second argument.
The third argument, which is optional, allows you to specify the desired date format.

For example, specifying it like this:

 convertFromUtc(utcNow(), 'Tokyo Standard Time', 'G') 
This converts UTC to Japan Standard Time (UTC +9 hours).
Example of converting UTC to Japan Standard Time

Convert from One Time Zone to Another: convertTimeZone

 convertTimeZone('timestamp', 'sourceTimeZone', 'destinationTimeZone', 'format'?) 

This function converts the time specified in the first argument from the time zone in the second argument to the time zone in the third argument.

The fourth argument, which is optional, allows you to specify the desired date format.

For example, specifying it like this:

 convertTimeZone('2021-01-27T16:45:00', 'Tokyo Standard Time', 'Hawaiian Standard Time', 'G') 
This converts Japan Standard Time to Hawaiian Standard Time.
Example of converting Japan Standard Time to Hawaiian Standard Time

Convert from a Specific Time Zone to UTC: convertToUtc

 convertToUtc('timestamp', 'sourceTimeZone', 'format'?) 

This function converts the time specified in the first and second arguments to UTC.

The third argument, which is optional, allows you to specify the desired date format.

The usage is almost identical to the previous functions, so it is omitted here.

Convert Date and Time to a Specified Format String: formatDateTime

 formatDateTime('timestamp', 'format') 

This function converts the time specified in the first argument into the format specified in the second argument.

In this function, specifying a format is mandatory.

For example, if you want to use a custom format for Japan Standard Time:

 formatDateTime(convertFromUtc(utcNow(), 'Tokyo Standard Time'), 'yy年MM月dd日のhh時mm分ss秒で曜日はdddd') 
The result will look like this:
Example of formatting Japan Standard Time with custom string

Add or Subtract Time Units from a Specified Date/Time: addToTime, subtractFromTime

 addToTime('timestamp', interval, 'timeUnit', 'format'?) subtractFromTime('timestamp', interval, 'timeUnit', 'format'?) 

This function adds or subtracts the time specified by the second and third arguments to the time provided in the first argument. The fourth argument for the format is optional.

The second argument should be specified as an integer, and the third argument should be chosen from the options below:

  • ‘Second’
  • ‘Minute’
  • ‘Hour’
  • ‘Day’
  • ‘Week’
  • ‘Month’
  • ‘Year’
For example, if you want to add one week to the current time in Japan, you would write:

 addToTime(convertFromUtc(utcNow(),'Tokyo Standard Time'), 1, 'Week', 'd') 

The result will look like this:
Example of adding one week to Japan Standard Time

By the way, functions like addDays or addHours work similarly to addToTime but without the third argument, so their usage is nearly identical.

Add or Subtract Time Units to/from the Current Date/Time: getFutureTime, getPastTime

 getFutureTime(interval, timeUnit, format?) getPastTime('timestamp', interval, 'timeUnit', 'format'?) 

These functions are similar to addToTime and subtractFromTime but omit the first argument, allowing you to add or subtract time directly from the current date and time.

Although not covered this time, functions like dayOfWeek are also quite useful and will be introduced in the future.

Related Articles

コメント

Copied title and URL