Power Automate provides functions to add time based on each of the units. For example, you can use the "addSeconds" function to add only seconds, but the "addToTime" function can provide a lot more flexibility since you define the unit that you want to add.
Usage
It follows a simple pattern.
- Date
- Number to add/remove
- Time unit to add
- (Optional) Date Format
The supported time units are: "Second", "Minute", "Hour", "Day", "Week", "Month", and "Year". Make sure you capitalize them exactly as shown in the documentation.
Here's a simple example:
addToTime('2019-10-28T10:10:00Z',10,'Second','yyyy-MM-ddTHH:mm:ssZ')
will return
'2019-10-28T10:10:10Z'
Looks quite straightforward, right? Positive numbers add time, and negative numbers subtract it. You can also use the "subtractFromTime" function for subtraction if you prefer to keep things explicit.
Please be aware that in the reference material, Microsoft names objects like '2019-10-28T10:10:00Z' as timestamps. I prefer calling them "Date" to avoid confusion with the UNIX timestamp, commonly used in APIs, to represent the number of seconds elapsed since Jan 01, 1970 (UTC).
Edge Cases
Adding months, weeks, and years
There are no "addMonths", "addWeeks", or "addYears" functions in Power Automate. If you need to add any of these units to a date, the "addToTime" function is the right way to do it. Here's an example adding 3 months:
addToTime('2019-10-28T10:10:00Z',3,'Month','yyyy-MM-ddTHH:mm:ssZ')
will return
'2020-01-28T10:10:00Z'
This is much simpler than trying to calculate the number of days in each month and using the "addDays" function.
When adding months, dates at the end of the month get clamped to the last valid day of the target month. For example, adding 1 month to January 31 gives February 28 (or February 29 in a leap year). The same applies when adding years: adding 1 year to February 29, 2024 gives February 28, 2025.
Be careful because this is not reversible. If you add 1 month to January 31 and get February 28, subtracting 1 month gives you January 28, not January 31. This can cause real bugs in billing or scheduling logic.
Limitations
Unlike SharePoint and Power Apps, there's no limitation to the date that you can use. You can do things like:
addToTime('1000-12-30T00:00:00Z',10,'Year','yyyy-MM-ddTHH:mm:ssZ')
and you'll get
1010-12-30T00:00:00Z
Dates behave like you're expecting them.
Recommendations
Here are some things to keep in mind.
Always include the format
Even if the value is optional, always define it. The default format includes fractional seconds and time zone information that you probably don't need, and the date provided may not be in the same format returned or even trigger errors. You can define a single format specifier (for example, "o") or a custom format pattern (for example, "yyyy-MM-dd"), so pick your favorite, but be sure to define it.
Prefer individual functions when they exist
When an individual function exists, like the "addSeconds" function, I prefer using it since it makes the formula quite clear and shorter. But for months, weeks, or years, the "addToTime" function is your only option.
Use "getFutureTime" and "getPastTime" for the current timestamp
If you just need to add or subtract time from the current timestamp, check out the "getFutureTime" and "getPastTime" functions. They do the same thing but use the current time automatically, so you don't need to provide a date.
Your Questions
Replacing the year while keeping the day and month
Paul asked a great question in the comments: is it possible to keep the same day and month from a date but always set the year to 1970? For example, converting 03.11.2000 to 03.11.1970. You can't simply subtract 30 years because the original year might change.
The trick is to calculate the difference between the original year and your target year, then use the "addToTime" function to adjust it:
addToTime('2000-11-03T00:00:00Z', sub(1970, int(formatDateTime('2000-11-03T00:00:00Z', 'yyyy'))), 'Year', 'yyyy-MM-ddTHH:mm:ssZ')
will return
'1970-11-03T00:00:00Z'
The "sub" function calculates 1970 - 2000 = -30, and the "addToTime" function subtracts those 30 years. Since we're calculating the difference dynamically, this works regardless of the original year. Replace the hardcoded date with your dynamic value and it will always land on 1970.
Keep in mind the month-end clamping mentioned above. If your original date is February 29 (a leap year) and your target year is not a leap year, the day will silently shift to February 28. If that's a problem, you can build the date as a string instead:
concat('1970-', formatDateTime('2000-11-03T00:00:00Z', 'MM-dd'), 'T00:00:00Z')
will return
'1970-11-03T00:00:00Z'
This approach skips the "addToTime" function entirely and just swaps the year in the string. However, if the original date is February 29 and the target year is not a leap year, this will generate an invalid date and trigger an error. So pick the approach that best fits your scenario: silent clamping with "addToTime" or an explicit error with "concat".
Final Thoughts
The "addToTime" function is a flexible way to manipulate dates in Power Automate by letting you choose the time unit to add. It's especially useful for adding months, weeks, or years, since there are no dedicated functions for those units. Just keep the month-end clamping behavior in mind if you're working with dates at the end of the month, and you'll be fine.
Sources
Microsoft's "addToTime" Function Reference
Back to the Power Automate Function Reference
Photo by NeONBRAND on Unsplash
Hello Is it possible to do things like "addToTime('2018-01-01T00:00:00Z', -1, 'Month')" or will that give you an error? Thank you
Hi Daniel, Yes it's correct and it works. <img src="https://manueltgomes.com/wp-content/uploads/2021/07/Screenshot-2021-07-28-at-12.27.55.png" alt="" /> The only thing that we need to be careful of is the quotes. They need to be straight single quotes for it to work. Cheers Manuel
Hi! Could you help? Is it possible From the date 03.11.2000, fix the same day and month, only the year will be 1970 (03.11.1970)? Minus 30 will not work, I want the year 1970 to always be like that, regardless of the original year.