July 15, 2025

The “dayOfYear function” helps us determine which day of the year a specific date falls on. This might not be the first function that comes to mind when working with Power Automate, but it’s incredibly useful when you need to perform date calculations, create reports based on days of the year, or implement business logic that requires knowing the exact position of a date within the year. Let’s explore how to use it effectively in your flows.

Where to find it?

You can find the function in every action where a formula is supported. For example, let’s look at a “Compose” action:

As you can see, we can auto-complete by using the “tab” key (old UI) or “enter” key (new UI). Let’s look at how to use it.

Usage

The dayOfYear function follows a simple pattern. It takes a single date input parameter and returns an integer between 1 and 366 (accounting for leap years) that represents the day of the year.

The basic syntax is:

dayOfYear(<date>)

Let’s start with a simple example using the current time:

dayOfYear(utcNow())

If today is March 6th, 2025, this would return 65 because March 6th is the 65th day of the year.

You can also use a specific date:

dayOfYear('2025-07-04')

This would return 185 because July 4 is the 185th day of the year.

I’m representing the dates as strings, but please check below to see how to define the dates better.

Limitations

There are a few things to be aware of when using the dayOfYear function:

  1. Time zone considerations

The day of the year will depend on the time zone and the date value. If you’re using utcNow function, you’re getting the UTC day, which might differ from the local day depending on your region. To get the regional day of the year, you might need to use convertTimeZone first.

  1. Expression size limits

As with all Power Automate expressions, there’s a limit of 8,192 characters. If you’re building complex date calculations, try to break them up into smaller, manageable pieces using variables.

Common Mistakes

Here are some common pitfalls when using the dayOfYear function:

Assuming all years have 365 days.

When using dayOfYear for calculations that compare dates across different years, remember that leap years have 366 days. Always check if the year is a leap year when needed. Here’s an example of 31st December 2024.

Forgetting to convert string dates

If you’re working with dates stored as strings, you need to convert them to date objects first:

// Incorrect
dayOfYear('2025-03-15')  // This might work but is not recommended

// Correct
dayOfYear(convertToUtc('2025-03-15', 'yyyy-MM-dd'))

It’s essential to keep the correct format so that you’re sure that the conversion is done correctly. I understand that this may look like it’s the same, but it’s not because of the ways that different regions/countries represent dates.

Not accounting for time zones

Different time zones might result in different days of the year for the same timestamp. Always be clear about which time zone you’re working with:

// UTC time
dayOfYear(utcNow())

// Eastern Time
dayOfYear(convertTimeZone(utcNow(), 'UTC', 'Eastern Standard Time'))

It is the same reason as before. Plus, having the timezone could make things more precise and let you know what day you are targeting, especially if you’re doing arithmetic on top of the result. Dates are complex, and there are a lot of hidden problems, so keeping things as precise as possible is essential.

Recommendations:

Here are some things to keep in mind when using the dayOfYear function:

Use “debug” compose actions

When building complex date calculations, use Compose actions as intermediate steps to see the values. This makes it easier to debug if something doesn’t work as expected.

Validate your dates

Before using dayOfYear, ensure your date values are valid. Try using a Compose action to output the date value first so you know what you’re working with.

Consider time zones

Always be intentional about time zones when working with dates. Document which time zone your flow expects and uses.

Always add a comment.

Adding a comment will help you and others understand your intention. Explain why you’re calculating the day of the year and how the result will be used. What might be apparent today may not be in a few months.

Use variables for complex expressions.

Instead of nesting dayOfYear inside complex expressions, store the result in a variable first, then use that variable in your logic. This improves readability and makes debugging easier.

Please don’t use it to calculate differences between dates.

When your logic spans multiple years, dayOfYear alone might not be sufficient or recommended. I would recommend never using the function to calculate differences between dates. Use the “dateDifference” function for such scenarios.

Don’t nest

If you nest this function, you would have an error, so there’s no real reason to do it. 

Sources:

Microsoft’s dayOfYear Function Reference

Back to the Power Automate Function Reference.

Photo by Sodbayar Photography on Unsplash

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon