Do you need to get the current year in your flow?
Getting the current year sounds like a simple task, but it’s not entirely obvious or easy to get.
The process involves using utcNow function to get the current date and time in UTC, converting UTC to your time zone, and formatting the date value as a year.
This post is going to show you how to get this done with expressions.
Getting Current Date and Time with utcNow Function
In Power Automate, you can easily obtain the current date and time using the
utcNow() function. This built-in function retrieves date and time values in the Coordinated Universal Time (UTC) format.
To use utcNow, simply include it in your expression wherever you need to get the current date and time. For example, you might use it within an action or condition in your Flow.
It does not require any arguments, so the syntax is as straightforward as the above expression.
The output for this expression will be a string in the format
yyyy-MM-ddTHH:mm:ss.fffffffK. This is a date and timestamp of the current date and time when the function is called.
yyyyis the year.
MMis the month.
ddis the day.
HHis the hour.
mmis the minutes.
ssis the seconds.
.fffffffis the fraction of a second. This is a decimal value between 0 to 1.
For example, 2023-07-06T19:13:20.7490143Z.
Converting UTC to Your Time Zone
If you are in a timezone other than UTC, you will need to convert the utcNow value to your own timezone before extracting the year.
This is because, on December 31st and January 1st, it’s possible for your timezone to be a different year than the UTC year.
Thankfully, it’s easy to convert UTC to any timezone with the convertTimeZone function.
convertTimeZone('<timestamp>', '<sourceTimeZone>', '<destinationTimeZone>', '<format>'?)
<timestamp<: The date and time to convert.
<sourceTimeZone>: The time zone of the original timestamp.
<destinationTimeZone>: The time zone to convert the timestamp to.
<format>: The format of the output timestamp. This parameter is optional.
convertTimeZone('2024-01-01T01:13:20.7490143Z', 'UTC', 'Eastern Standard Time', 'yyyy-MM-dd HH:mm')
For example, the above expression will convert a date and timestamp in UTC to EST. This also uses the optional format argument to return the result as a date and time without any seconds or any fraction of seconds.
This example will return the result 2023-12-31 20:13.
📝 Note: This example will return the result 2023-12-31 20:13. The result is a different year than the original date due to the original date and time being on January 1st.
Formatting Current Date and Time Value as a Year
Since you will need to convert the utcNow result into your timezone with the convertTimeZone function, you can also format the result as a year at the same time.
convertTimeZone(utcNow(), 'UTC', 'Eastern Standard Time', 'yyyy')
For example, the above expression will return only the current year in EST.
utcNow()gets the current date and time in Coordinated Universal Time (UTC).
- ‘UTC’ specifies the date and timestamp used is UTC.
'Eastern Standard Time'specified that the date and timestamp should be converted to EST.
'yyyy'part of the code specifies the custom format string to display only the year as a four-digit number.
This result will be a string value, but you might want to convert it to an integer value. This can be easily done with the int function.
int(convertTimeZone(utcNow(), 'UTC', 'Eastern Standard Time', 'yyyy'))
The above expression will return only the current year in EST as an integer data type. You can use this expression anywhere you want to get the current year!
In Power Automate, it’s possible to retrieve the current year from the utcNow function
You will need to convert the current date time to your timezone and then format the results as a year.
Optionally, you can convert the year string to a year integer if required for further calculations.
Hopefully this has helped you get today’s year into your flows. Let me know in the comments!