Power Automate getPastTime() Function

This Power Automate tutorial will explain how to work with the getPastTime() function. In addition, this blog post will guide you on the Power Automate getPastTime() function, its syntax, and a few examples.

Power Automate has the getPastTime() function, which helps subtract the specific time units from the current timestamp units like seconds, minutes, hours, days, weeks, months, or years based on the unique needs to get the passed time.

Power Automate getPastTime() Function

In Power Automate, Microsoft has a helpful function called “getPastTime(),” designed to subtract specific time of units from a current timestamp. This function allows users to subtract different time units, such as seconds, minutes, hours, days, weeks, months, or years, as needed.

The return value from the getPastTime() function will be a timestamp minus the specified number of time units.

Let’s say today’s date is 17-12-2023, with the current time as T08:35:56. I wanted to get the past 10 seconds to the current time. Then, my result will be 2023-12-17T08:35:46.

Power Automate Get past time () function

This is a brief introduction to the Power Automate getPastTime() function.

Power Automate getPastTime() Syntax

The syntax for a Power Automate getPastTime() function looks like this:

getPastTime('interval', 'timeunit', 'format')

Parameters:

  • Interval: It is in the form of an integer where a particular number of time units are added.
  • Time Unit: It represents the unit of time that can be used with intervals like Second, Minute, Hour, Day, Week, Month, and Year.
  • Format: It is an optional field where a user can provide a custom timestamp format. It will take the default format[yyyy-MM-ddTHH:mm: ss.fffffffK] if not provided.

Example:

For example, I will give 1 day as a time unit to the current date. Let’s say the current date is”17-12-2023“.

getPastTime(1, 'Day')
  1. 1 = Specify a particular interval of time unit.
  2. Day = Provide the time unit like Second, Minute, Hour, Day, Week, Month, and Year per your need.

Here will be the output as 2023-12-16T00:00:00.0000000Z.

This is the overview of Power Automate’s getPastTime() function syntax.

Power Automate getPastTime in Seconds

Now, let me explain an example of getting past the time of seconds in Power Automate flow using the getPastTime() function.

Example:

Assuming the current date is December 17th, 2023, and the current time is 08:35:56. I need to retrieve the period 10 seconds before the current time. Consequently, the resulting time would be ‘17-12-2023T08:35:46.

Power Automate getPastTime() function

To achieve this, follow the below given steps.

1. Navigate to Power Automate -> Tap on +Create -> Select Instant cloud flow -> Take Manually trigger a flow and enter a flow name -> Click Create.

  • Open the trigger -> Click Add an input -> Select Date.
getPastTime() function in Power Automate

2. Add an ‘Initialize variable’ flow action under the trigger. The Initialize variable stores the information of the date, and the stored values can be used during the flow.

Set the parameters of the initialized variable.

  • Name: Enter the variable name manually as Past Time in Seconds.
  • Type: Select String datatype.
  • Value: Click the field -> Click Expression -> Insert the below code.
getPastTime(10,'Second','yyyy-MM-ddTHH:mm:ss')

After that, Click OK.

Power Automate getPastTime() Function

 3. Take a Compose flow action and set the variables of initialize variable in the inputs section as below.

  • Inputs: Select the field -> Take variable from the dynamic content.
Power Automate getPastTime() expression

4. After creating the flow, Save and Test the flow manually. Pick the triggered date in the Run flow window to get past time. Then, click Run flow.

Microsoft Power Automate getPastTime() function

5. Now the flow runs successfully, it will notify a message as Your flow ran successfully. Then, open the compose flow action to see the output. The outputs will display the returns of the getPastTime() expression as below.

  • The return output will be in the past 10 seconds to the current 2023-12-17T08:35:46 [Current date and time 2023-12-17T08:35:56].
Power Automate getPastTime in Seconds

This is how to get the past time in seconds using a Power Automate getPastTime() function.

Power Automate getPastTime() in Minutes

In the same way, we will see about Power Automate getPastTime() in the minutes time unit.

Example:

In the same way, I needed to subtract a designated number of minutes from the current date and time. Thus, the updated Today’s date and time will reflect past time in Minutes.

Assuming today’s date is “17-12-2023,” adding 20 minutes to the current time will result in 2023-12-17T12:19:14, as seen in the screenshot below. Presently, the current time is [2023-12-17T12:39:14].

Power Automate getPastTime() in Minutes

As mentioned above, follow the same steps but only change the getPastTime() expression.

1. Add the Initialize variable flow action to take the getPastTime () expression. Set the below given parameters in the variable.

  • Name: Enter the variable name manually as Date and Time.
  • Type: Select datatype String from drop-down. 
  • Value: Select the field -> Click Expression -> Take the code below.
getPastTime(20,'Minute','yyyy-MM-ddTHH:mm:ss')

After giving the expression, Click OK.

Get Power Automate past time in Minutes

2. After running the flow successfully, the output will be retrieved from the compose flow action by adding minutes to the timestamp as below. The result will be 2023-12-17T12:19:14 (past 20 minutes).

Power Automate getPastTime in Minutes

This is about how to get past time in minutes of the current date and time using the Power Automate getPastTime() function.

Power Automate getPastTime() in Hours

Similarly, in a flow, let’s see how to get the past time in hours to the current date and time using a Power Automate getPastTime() function.

Example:

I will add a specific number of hours to a current timestamp. For instance, suppose we have a current date and time as of “2023-12-18T05:04” and need to give 2 hours. In that case, the resulting timestamp will be “2023-12-18T03:04,” which will give the past time in hours, as shown below.

Get Power Automate getPastTime() in Hours

Follow the steps below:

1. Inside the ‘Initialize variable‘ flow action, give Name, Type, and a Value. Set the details:

  • Name: Provide the variable name manually as Current Date.
  • Type: Select datatype String.
  • Value: Select field -> Take Expression -> Provide the code below.
getPastTime(2,'Hour','yyyy-MM-ddTHH:mm')

After that, Click OK.

Get Microsoft Power Automate getPastTime() in Hours

2. When the flow runs successfully, open the compose flow action to check the outputs retrieved will contain the past time in its hours, as shown below.

  • The result of the Power Automate getPastTime() expression will be 2023-12-18T03:04.
Power Automate getPastTime() in Hours

This will be a detailed process of using the Power Automate getPastTime() function in hours from the current timestamp.

Power Automate getPastTime() in Days

Going deep into the Power Automate getPastTime() function, let’s look at how to get the past days from the current time.

Follow the same steps that are mentioned in the above example:

  • Add a manual trigger flow to take the input as ‘Date.’
  • Provide the getPastTime() expression in the ‘Compose’ data operation.

Example:

This example involves adding a specific number of days to a current timestamp. For example, if we begin with a timestamp of “2023-12-18” and intend to get the past second day, the resulting timestamp will be “2023-12-16,” as shown below.

Get Power Automate getPastTime() function in Days

Go through the following steps that are mentioned below:

1. In the Inputs of the compose flow action, give the expression to get past days from the current timestamp.

Inputs: Select the field -> Click Expression -> Add the expression given below.

getPastTime(2,'Day')

After that, Click OK.

Power Automate getPastTime() in Days

2. Once the flow has been saved, select the desired date for the unit to be added in the “Run flow” window. Then, click Run flow.

Microsoft Power Automate getPastTime() function in days

3. The image below illustrates the getPastTime() function results, which subtracts two days from the current timestamp.

  • The result will be 2023-12-16.
Power Automate getPastTime() function in Days

This is about the Power Automate getPastTime() function to retrieve the past days from the current time date.

Power Automate getPastTime() in Months

In this section, let’s look at the example that will add a month to the timestamp using a Power Automate addToTime() function.

Example:

Let us assume the current timestamp is “2023-12-18“. I wanted to get the past 4 months to this current date. So, the return output will be like ”August 18, 2023,” which means it will subtract the months provided in the getPastTime() expression.

Microsoft Power Automate getPastTime() in Months

To work around this, follow the steps below:

1. Take Month as a time unit in the getPastTime() expression so that it will subtract given intervals of months from the current timestamp in the compose flow action.

Inputs: Select the field -> Click Expression -> Give the expression given below.

getPastTime(4,'Month','MMMM dd, yyyy')

Then, Select OK.

The 4 months will be subtracted from the specific time units of the original timestamp provided.

Power Automate getPastTime() in Months

2. After that, Save and Test the flow manually. In the Run flow window, pick a triggered date. Click Run flow.

Power Automate  flow getPastTime() function in days

3. Look out for the timestamp retrieved from the getPastTime() expression in the compose outputs, displaying the given current date minus the specified number of time units.

  • The return output will be August 18, 2023.
getPastTime() in Months Microsoft Power Automate

This is all about how to get past time in months using the Power Automate getPastTime() function.

Power Automate getPastTime() in Year

Let us see the final example, i.e., getting a past year from the current timestamp using a Power Automate getPastTime() expression inside a flow.

Example:

Let’s give the timestamp “2023-12-18“. I need to subtract 10 years from the current timestamp that we have taken. So, the return output will be like ”2013-12-18,” which means subtracting the given years provided in the getPastTime() expression from the timestamp.

Power Automate getPastTime in Year

To do this example, follow the below given steps.

1. Similarly, change the format of getPastTime() expression to include a year. Provide the below-given code in the Inputs of the compose flow action.

Inputs: Click the field -> Select Expression ->Take the code given below.

getPastTime(10,'Year','yyyy-MM-dd')

Click Ok.

Power Automate flow getPastTime() function in Years

2. Once the flow has been created, save and run it. In the Runflow window, pick the triggered date you need, as shown below. Click Run flow.

Microsoft Power Automate getPastTime() function in years

3. The Power Automate getPsstTime() function will give the output relevant to the expression by subtracting years from the current timestamp.

  • The return value from the getPastTime() expression is 2013-12-18.
Power Automate getPastTime() function in Years

This is all about getPastTime() expressions and how to use them in a Power Automate flow.

Conclusion

From this tutorial, I hope you better understand how to use the Power Automate getPastTime() expression using various examples.

Along with this, I have also covered the below topics with the getPastTime() function:

  • Power Automate getPastTime in second
  • Power Automate getPastTime in minutes
  • Power Automate getPastTime in an hour
  • Power Automate getPastTime in day
  • Power Automate getPastTime in month
  • Power Automate getPastTime in the year

Moreover, you may like some more Power Automate tutorials: