Skip to main content

Notifications

Community site session details

Community site session details

Session Id : bpozjnbwlW0jS1Q1fHgyMV
Power Automate - Building Flows
Unanswered

Subtract two dates and times

Like (0) ShareShare
ReportReport
Posted on 26 Apr 2023 12:22:15 by

Hello everyone,

 

I want to subtract two dates from each other, but I get this error:

mtndnmz_0-1682511656903.png

The format I use to subtract dates is like this: div(sub(ticks(formatDateTime(addHours(utcNow(),3)),'yyyy-MM-ddTHH:mm:ss')),ticks(formatDateTime(item()?[ 'X'])),'yyyy-MM-ddTHH:mm:ss'),864000000000)

  • David_MA Profile Picture
    10,607 Super User 2025 Season 1 on 26 Apr 2023 at 15:01:46
    Re: Subtract two dates and times

    The blog post I posted (How to calculate difference between two times in Power Automate (tomriha.com)) will show you what you need to do. The calculation can only calculate it to one thing, either days, hours, minutes OR seconds. Since you need to know if it has been more than 30 minutes, use the minutes calculation and you can use it in a condition to see if the result is greater than 30.

  • Phonic_Shihawu2 Profile Picture
    14 on 26 Apr 2023 at 14:58:26
    Re: Subtract two dates and times

    Add a "Compose" action to your flow.
    In the "Inputs" field of the "Compose" action, add the following expression:
    subtractFromTime(utcNow(), variables('startDate'), 'HH:mm')

    Replace "variables('startDate')" with the date you want to subtract from (e.g. a SharePoint column that contains a date/time value).
    3. The result of the "Compose" action will be the time difference between the two dates in the format "HH:mm". You can then use this value in other actions in your flow.

  • Community Power Platform Member Profile Picture
    on 26 Apr 2023 at 14:44:04
    Re: Subtract two dates and times

    I need to get the difference not only in date but also in hours and minutes. If there is more than 30 minutes between the current date and the current date for each item, I will process them. Do I need to do the formula you mentioned in the first paragraph in hours and minutes?

  • David_MA Profile Picture
    10,607 Super User 2025 Season 1 on 26 Apr 2023 at 14:17:15
    Re: Subtract two dates and times

    I suspect you are trying to determine the difference in time between two dates rather than actually subtracting one from the other. If this is the case, the following expression will tell you how many days have elapsed between the Created and Modified dates:

     

    div(sub(ticks(formatDateTime(triggerBody()?['Modified'],'yyyy-MM-dd')),ticks(formatDateTime(triggerBody()?['Created'],'yyyy-MM-dd'))),864000000000)

     

    This is a good blog post (How to calculate difference between two times in Power Automate (tomriha.com)) to show you how to get days, hours, minutes and seconds between two dates.

     

    If you just want to subtract x number of days from a date, you would use the addDays expression and enter a negative number:

     

    addDays(items('Apply_to_each')?['Modified'],-3,'yyyy-MM-dd')

     

     

     There are expressions for addHours, addMinutes and addSeconds as well. If you want to subtract, use a negative number.

  • Community Power Platform Member Profile Picture
    on 26 Apr 2023 at 14:04:39
    Re: Subtract two dates and times

    I am still getting the same error. I guess the problem is not where I use utcNow, but in the time zone of the data that I use as the 2nd date.

  • annetoal Profile Picture
    1,934 Super User 2025 Season 1 on 26 Apr 2023 at 13:59:37
    Re: Subtract two dates and times

    Try this instead

    formatDateTime(utcNow(),'d MMM yyyy hh:mm:ss tt')

    Substituting whatever time string you need for where I put utcNow().

     

    If this helped, please mark it as a Solution,

    Anne

  • Community Power Platform Member Profile Picture
    on 26 Apr 2023 at 13:56:03
    Re: Subtract two dates and times

    In order for me to subtract two dates from each other, I guess they have to be equal. I use the format yyyy-MM-ddTHH:mm:ss in the utcNow function I use for the first date. On my second date, the data I received is 20230426 000900.000. I guess I should parse it first, but I was unsuccessful.

  • annetoal Profile Picture
    1,934 Super User 2025 Season 1 on 26 Apr 2023 at 13:49:52
    Re: Subtract two dates and times

    How do you want the datetime to appear?

     

     

     

     

  • Community Power Platform Member Profile Picture
    on 26 Apr 2023 at 13:41:44
    Re: Subtract two dates and times

    The error I get is as follows:

    Unable to process template language expressions in action 'Compose_2' inputs at line '0' and column '0': 'In function 'formatDateTime', the value provided for date time string '20230426 000900.000' was not valid. The datetime string must match ISO 8601 format.'.

  • annetoal Profile Picture
    1,934 Super User 2025 Season 1 on 26 Apr 2023 at 13:24:51
    Re: Subtract two dates and times

    Can't read your error message--

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,660 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,999 Most Valuable Professional

Leaderboard
Loading started