how to get difference between two datetime expressions set as a variable

Martin Kallukalam 355 Reputation points
2024-08-07T21:04:32.4466667+00:00

I want to write a variable which calculate time to execute a policy. My policy goes like this
I tried MS copilot but the code snippet it is giving out is all syntactically wrong and APIM wouldnt acccept it.

<inbound>

        <base />

        <set-variable name="tstartretrypolicy1" value="@(DateTime.UtcNow)" />

        <retry condition="@(true)" count="1" interval="2" />

        <set-variable name="tsendretrypolicy1" value="@(DateTime.UtcNow)" />

    <set-variable name="texecretrypolicy1" value="@((context.Variables["tsendretrypolicy1"] - context.Variables["tstartretrypolicy1"]))" />

I get error
Error in element 'set-variable' on line 14, column 2: Operator '-' cannot be applied to operands of type 'object' and 'object'

how do I substract two datetime expressions and assign it to a variable?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,105 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 26,866 Reputation points
    2024-08-08T10:41:01.2466667+00:00

    @Martin Kallukalam Thanks for reaching out. The error you’re encountering is because the subtraction operator - cannot be directly applied to objects in the way you’re attempting.

    please refer the below policy

    <inbound>
        <base />
        <set-variable name="tstartretrypolicy1" value="@(DateTime.UtcNow)" />
        <retry condition="@(true)" count="1" interval="2" />
        <set-variable name="tsendretrypolicy1" value="@(DateTime.UtcNow)" />
        <set-variable name="texecretrypolicy1" value="@(Convert.ToDateTime(context.Variables["tsendretrypolicy1"]) - Convert.ToDateTime(context.Variables["tstartretrypolicy1"]))" />
    </inbound>
    

    Hope this helps! do let me know incase of further queries, I would be happy to assist you.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.