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
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 seconds, and negative exclude them.
Please be aware that 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).
Limitations
Contrary to SharePoint and PowerApps, 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:
- Always include the format, even if the value is optional. The default value is “yyyy-MM-ddTHH:mm:ss:fffffffK,” but 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.
- I prefer to use individual functions instead of this function. For example, I prefer using the addSeconds Function since it makes the formula quite clear and shorter. It’s a matter of preference since the result will be the same.
Sources:
Microsoft’s addToTime Function Reference
Back to the Power Automate Function Reference
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.
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.