简体   繁体   中英

Compare two dates in logic apps

I have two date objects where I need to find the duration. My question here is: How can I calculate duration between two date strings in logic apps?

You can use ticks() in order to compare two date strings. Ticks return the ticks property value for a specified timestamp.

syntax of ticks()

ticks('<timestamp>')

I have considered 2 dates (ie, 2022-01-22T22:01:37Z,2022-02-20T22:01:37Z) for instance.

在此处输入图像描述

Then in the Calculate Duration connector I'm using the below expression

div(sub(ticks(outputs('Sample_Date1')),ticks(outputs('Sample_Date_2'))),864000000000)

The reason we divide by 864000000000 is that there are 100 * 100 * 100 * 60 * 60 * 24 ticks in a single day. In a nutshell, comparing two dates requires nothing more than a tick-to-day conversion. Hence as a result we get number of days as duration.

output:

在此处输入图像描述

Here is the code view of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Calculating_Duration": {
                "inputs": "@div(sub(ticks(outputs('Sample_Date_2')),ticks(outputs('Sample_Date1'))),864000000000)",
                "runAfter": {
                    "Sample_Date_2": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Sample_Date1": {
                "inputs": "2022-01-22T22:01:37Z",
                "runAfter": {},
                "type": "Compose"
            },
            "Sample_Date_2": {
                "inputs": "2022-02-20T22:01:37Z",
                "runAfter": {
                    "Sample_Date1": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

REFERENCE: Reference guide for expression functions - Azure Logic Apps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM