DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday: Date and Time Addition

This time I’m going to start delving into the date and time functions of Power Automate. Date and time is one of the most painful parts of working with flows.

The reason for that is that sometimes a DateTime value is a DateTime, sometimes it’s a number, and sometimes it’s a string. Usually it’s a string… mostly. And it’s hard to know which it is at any given moment. Then add to that plethora of different formats that a DateTime value might take to and from all the various external systems and you have another layer of complexity. And for yet one more level of complexity, we’ll add in the issue of timezones.

For this post, we’re going to start simple: Date and time addition.

addDays, addHours, addMinutes, addSeconds

These four functions all work in the exact same manner. Each function takes three input parameters. The first two parameters, timestamp, and value are required. The third parameter, format, is optional.

The functions present as follows:

addDays('<timestamp>', <value>, '<format>')
addHours('<timestamp>', <value>, '<format>')
addMinutes('<timestamp>', <value>, '<format>')
addSeconds('<timestamp>', <value>, '<format>')
Enter fullscreen mode Exit fullscreen mode

Timestamp is the input value, the DateTime that you are starting with. This is passed in as a string.

The value parameter is the amount to add in days, hours, minutes, or seconds. This value can be positive or negative. There are no “subtract” functions, so if you need to go the opposite direction, you use the add functions with a negative value.

The default assumed format for timestamp is ISO 8601, which is presented as “yyyy-MM-ddTHH:mm:ss.fffffffK”.

  • yyyy represents the year: 2022
  • MM represents the month. With all values, single digit values have a leading zero: 08
  • dd represents the day of the month: 12
  • T is a placeholder that sits in between the date and time portions of the value
  • HH represents the hours in military time: 14
  • mm represents the minutes: 00
  • ss represents the seconds: 05
  • fffffff represents micro-seconds: 0051332
  • K represents the timezone. UTC is represented by a Z

If the timestamp value is not in that format (say, for example, it is passed in the format of “MM/dd/yyyy” or something else, you’ll need to specify what that format is in this parameter. You can either pass in one of the standard formats or define a custom format. You can read more about date formats in the docs here.

The output result is always in the same format as the input timestamp. If you need to change the output format, you’ll have to pass the output into a formatDateTime function, which I’ll cover in another post.

Some examples:

addDays('2022-08-11T12:00:00.0000001Z', 1) // returns '2022-08-12T12:00:00.0000001Z'

addHours('2022-08-11T12:00:00.0000000Z', 3) // returns '2022-08-11T15:00:00.0000000Z'

addMinutes('2022-08-11T12:00:00Z', 13) // returns '2022-08-11T12:13:00Z'
Enter fullscreen mode Exit fullscreen mode

addToTime

The addToTime function is a bit of a more generic function. It allows you to take a timestamp and add or subtract a number of seconds, minutes, hours, days, weeks, months, or years. The pattern is as follows:

addToTime('<timestamp>', <interval>, '<unit>', 'format')
Enter fullscreen mode Exit fullscreen mode

As with the other “add” functions, format is an optional parameter. The other three parameters are required.

Timestamp is the exact same as with the other functions. Interval is the amount of time to add to the timestamp. It can be a positive or negative value.

Unit tells the function what type of interval to add. The allowed values are: “Second”, “Minute”, “Hour”, “Day”, “Week”, “Month”, “Year”.

An example:

addToTime('2022-08-11', 10, 'Year') // returns '2032-08-11'
Enter fullscreen mode Exit fullscreen mode

Conclusion

There’s a lot to working with dates and times in Power Automate and we’ve just begun jumping into the mess that it is. Next time I’ll take a look a more in-depth look at formatting and parsing.

The post Function Friday: Date and Time Addition first appeared on Barret Codes.

Top comments (0)