October 31, 2024

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.

  1. Date
  2. Number to add/remove
  3. Time unit to add
  4. (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:

  1. 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.
  2. 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

Featured Image by NeONBRAND on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

3 thoughts on “Power Automate: addToTime Function

  1. 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

    1. 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

  2. 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.

Leave a Reply

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

Mastodon