Code Explainer

Formatting DateTime in Power Automate

This guide explains how to convert and format the current UTC time into New Zealand Standard Time using Power Automate functions, providing a clear understanding of utcNow(), convertFromUTC(), and formatDateTime().


Empty image or helper icon

Prompt

formatDateTime(convertFromUTC(utcNow(), 'New Zealand Standard Time'), 'yyyy-MM-dd')

Answer

Code Explanation: Format DateTime in Power Automate

Code Breakdown

The provided code snippet is a function call commonly used in Microsoft Power Automate (previously known as Microsoft Flow). It combines three functions to convert the current UTC time to a local time zone and format it according to a specified pattern.

Code Structure

formatDateTime(convertFromUTC(utcNow(), 'New Zealand Standard Time'), 'yyyy-MM-dd')

Function Components

  1. utcNow()

    • Purpose: Returns the current date and time in Coordinated Universal Time (UTC).
    • Output: A datetime object representing the current moment.
  2. convertFromUTC()

    • Purpose: Converts the UTC datetime returned by utcNow() to a specified time zone, which in this case is 'New Zealand Standard Time'.
    • Arguments:
      • The first argument is the UTC datetime.
      • The second argument is a string indicating the desired time zone.
    • Output: A datetime object adjusted to the specified local time zone.
  3. formatDateTime()

    • Purpose: Formats a datetime object into a specific string format.
    • Arguments:
      • The first argument is the datetime object to format, which is the output from convertFromUTC().
      • The second argument is a format string, 'yyyy-MM-dd', defining the desired date format.
    • Output: A string representation of the date in the specified format.

Overall Functionality

  • The entire line of code will:
    1. Retrieve the current UTC date and time.
    2. Convert this datetime to New Zealand Standard Time.
    3. Format this converted date such that only the date portion (year, month, day) is returned as a string (e.g., "2023-10-31").

Key Concepts Explained

  • UTC (Coordinated Universal Time): A time standard that serves as the basis for civil timekeeping worldwide. It does not change with seasons and is not affected by daylight saving time.
  • Time Zones: Regions of the world that have the same standard time. 'New Zealand Standard Time' is the time observed in New Zealand when not observing daylight saving time.
  • Date Formatting: The process of converting a datetime object into a human-readable string format. The 'yyyy-MM-dd' format represents a four-digit year, two-digit month, and two-digit day.

Additional Example

To illustrate a similar concept using a different time zone and format, consider the following code:

formatDateTime(convertFromUTC(utcNow(), 'Pacific Standard Time'), 'MM/dd/yyyy')
  • Explanation: This code converts the current UTC time to Pacific Standard Time and formats it as "month/day/year".

Conclusion

The given code snippet effectively retrieves, converts, and formats the current UTC time into a local format suitable for applications requiring local datetime representation. Understanding these functions is crucial for tasks related to time-sensitive data in automation workflows, particularly in Microsoft Power Automate.

For further learning on such concepts and their applications, exploring courses offered by the Enterprise DNA platform may be beneficial.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This guide explains how to convert and format the current UTC time into New Zealand Standard Time using Power Automate functions, providing a clear understanding of utcNow(), convertFromUTC(), and formatDateTime().