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

如何在curl请求中传递当前时间并添加5小时?endsAt语法报错排查

解决curl请求中endsAt时间参数的语法问题

直接修正date命令语法

你的endsAt参数对应的date命令存在小问题,-d参数后的时间表达式不需要多余空格,调整后即可正常运行:

curl 'http://test.com' -d '{
      "matchers": [
      {
         "name": "test",
         "value": "test",
         "isRegex": true
      }
       ],
        "startsAt": "'"$(date +"%Y-%m-%dT%H:%M:%SZ")"'", 
        "endsAt": "'"$(date -d "+5 hours" +"%Y-%m-%dT%H:%M:%SZ")"'", 
        "createdBy": "test",
        "comment": "test",
        "status": {
        "state": "active"
      }}'

将'+ 3 hours'改为"+5 hours"(去掉表达式内的空格,用双引号包裹避免shell解析异常),date就能正确计算当前时间加5小时的结果。

用变量存储时间(更可靠的方式)

如果想要startsAt和endsAt基于同一个初始时间(避免两次执行date期间时间跳动导致时差不准),可以先把时间存入变量再使用:

# 获取当前时间
current_time=$(date +"%Y-%m-%dT%H:%M:%SZ")
# 计算当前时间加5小时后的时间
ends_time=$(date -d "$current_time +5 hours" +"%Y-%m-%dT%H:%M:%SZ")

# 发起curl请求
curl 'http://test.com' -d '{
      "matchers": [
      {
         "name": "test",
         "value": "test",
         "isRegex": true
      }
       ],
        "startsAt": "'"$current_time"'", 
        "endsAt": "'"$ends_time"'", 
        "createdBy": "test",
        "comment": "test",
        "status": {
        "state": "active"
      }}'

这种方式能保证startsAt和endsAt的时差严格为5小时,不会因为两次date命令的执行间隔(比如刚好跨分钟)出现偏差。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 07:37:22