You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Logic App中Epoch时间戳转换异常问题排查

Azure Logic App Epoch时间戳转换异常修复

问题场景

处理形如/Date(时间戳)/格式的日期字符串时,拆分提取时间戳数字后,使用addToTime函数转换得到的日期与预期完全不符。

输入输出对比

原始输入

"EndDate": "/Date(253402214400000)/",
"StartDate": "/Date(946684800000)/"

预期输出

"EndDate"  : "2000-01-01T00:00:00",
"StartDate": "9999-12-31T00:00:00"

当前错误输出

"EndDate": "1942-11-15T20:26:40.0000000Z",
"StartDate": "2026-10-14T16:21:20.0000000Z"

错误原因

你的时间戳是毫秒级Epoch时间,但当前addToTime函数使用的单位是Second,相当于把毫秒数直接当成了秒数计算,导致时间偏移量被放大1000倍,最终得到完全错误的日期:

  • 946684800000毫秒对应2000-01-01,当成秒计算时变成946684800000秒,对应2026年之后的错误日期
  • 253402214400000毫秒对应9999-12-31,当成秒计算时超出合理时间范围,得到1942年的错误日期

修复方案

两种可行修复方式,推荐第一种更直接:

方式1:修改addToTime的时间单位为毫秒

在Compose_3的表达式中,将addToTime的第三个参数改为'Millisecond',直接用毫秒数计算:

"EndDate": "@{addToTime('1970-01-01T00:00:00Z', int(body('Parse_JSON_2')?['root']?['EndDate1']), 'Millisecond')}",
"StartDate": "@{addToTime('1970-01-01T00:00:00Z', int(body('Parse_JSON_2')?['root']?['StartDate1']), 'Millisecond')}"

方式2:将提取的毫秒数转换为秒

在提取时间戳后,先除以1000转换成秒数,再传入addToTime:

"EndDate1": "@{int(div(replace(replace(body('Parse_JSON')?['root']?['EndDate'],'/Date(',''),')/',''), 1000))}",
"StartDate1": "@{int(div(replace(replace(body('Parse_JSON')?['root']?['StartDate'],'/Date(',''),')/',''), 1000))}"

修正后的完整Logic App代码

{    
    "definition": {        
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",        
        "actions": {            
            "Compose": {                
                "inputs": {                    
                    "root": {                        
                        "CostAccountID": "AD100",                        
                        "EndDate": "9999-12-31T00:00:00Z",                        
                        "StartDate": "2000-01-01T00:00:00Z"                    
                    }                
                },                
                "runAfter": {},                
                "type": "Compose"            
            },            
            "Compose_2": {                
                "inputs": {                    
                    "root": {                        
                        "CostAccountID": "@{body('Parse_JSON')?['root']?['CostAccountID']}",                        
                        "EndDate1": "@{int(replace(replace(body('Parse_JSON')?['root']?['EndDate'],'/Date(',''),')/',''))}",                        
                        "StartDate1": "@{int(replace(replace(body('Parse_JSON')?['root']?['StartDate'],'/Date(',''),')/',''))}"                    
                    }                
                },                
                "runAfter": {                    
                    "Parse_JSON": [                        
                        "Succeeded"                    
                    ]                
                },                
                "type": "Compose"            
            },            
            "Compose_3": {                
                "inputs": {                    
                    "root": {                        
                        "CostAccountID": "@{body('Parse_JSON')?['root']?['CostAccountID']}",                        
                        "EndDate": "@{addToTime('1970-01-01T00:00:00Z', int(body('Parse_JSON_2')?['root']?['EndDate1']), 'Millisecond')}",                        
                        "StartDate": "@{addToTime('1970-01-01T00:00:00Z', int(body('Parse_JSON_2')?['root']?['StartDate1']), 'Millisecond')}"                    
                    }                
                },                
                "runAfter": {                    
                    "Parse_JSON_2": [                        
                        "Succeeded"                    
                    ]                
                },                
                "type": "Compose"            
            },            
            "Parse_JSON": {                
                "inputs": {                    
                    "content": "@outputs('Compose')",                    
                    "schema": {                        
                        "properties": {                            
                            "root": {                                
                                "properties": {                                    
                                    "CostAccountID": {                                        
                                        "type": "string"                                    
                                    },                                    
                                    "EndDate": {                                        
                                        "type": "string"                                    
                                    },                                    
                                    "StartDate": {                                        
                                        "type": "string"                                    
                                    }                                
                                },                                
                                "type": "object"                            
                            }                        
                        },                        
                        "type": "object"                    
                    }                
                },                
                "runAfter": {                    
                    "Compose": [                        
                        "Succeeded"                    
                    ]                
                },                
                "type": "ParseJson"            
            },            
            "Parse_JSON_2": {                
                "inputs": {                    
                    "content": "@outputs('Compose_2')",                    
                    "schema": {                        
                        "properties": {                            
                            "root": {                                
                                "properties": {                                    
                                    "CostAccountID": {                                        
                                        "type": "string"                                    
                                    },                                    
                                    "EndDate1": {                                        
                                        "type": "string"                                    
                                    },                                    
                                    "StartDate1": {                                        
                                        "type": "string"                                    
                                    }                                
                                },                                
                                "type": "object"                            
                            }                        
                        },                        
                        "type": "object"                    
                    }                
                },                
                "runAfter": {                    
                    "Compose_2": [                        
                        "Succeeded"                    
                    ]                
                },                
                "type": "ParseJson"            
            },            
            "Response": {                
                "inputs": {                    
                    "body": "@outputs('Compose_3')",                    
                    "statusCode": 200                
                },                
                "kind": "http",                
                "runAfter": {                    
                    "Compose_3": [                        
                        "Succeeded"                    
                    ]                
                },                
                "type": "Response"            
            }        
        },        
        "contentVersion": "1.0.0.0",        
        "outputs": {},        
        "triggers": {            
            "manual": {                
                "inputs": {},                
                "kind": "Http",                
                "type": "Request"            
            }        
        }    
    },    
    "kind": "Stateful"
}

注:同时修正了Response步骤的输出为Compose_3的结果,确保返回正确转换后的日期。

内容的提问来源于stack exchange,提问作者PeterS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:35:24