Hello everyone,
I want to subtract two dates from each other, but I get this error:
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)
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.
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.
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?
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.
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.
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
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.
How do you want the datetime to appear?
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.'.
Can't read your error message--
WarrenBelz
146,660
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
65,999
Most Valuable Professional