【问题标题】:Calculating age in JSON IBM Watson Assistant在 JSON IBM Watson Assistant 中计算年龄
【发布时间】:2019-02-11 21:53:34
【问题描述】:

我在 Watson 的 JSON 编辑器中执行简单的日期计算时遇到了一些困难。我有一个已知值,即日期:2018 年 7 月 27 日,我需要做的是根据当前日期计算并输出以月和日为单位的年龄。我试图根据表达式语言方法中给出的示例来解决这个问题,但我没有成功。

我需要做的是将 (new Date(2019, 2, 13)).getTime() 替换为 today() 之类的内容,以便在有人要求时实时计算月龄年龄,我尝试用 Today() 替换它,但它会导致错误...

{
  "context": {
    "days": "<? (((new Date(2019, 2, 13)).getTime() - (new Date(2018, 7, 29)).getTime()) / (1000 * 60 * 60 * 24)) / 30 ?>"
  },
  "output": {
    "generic": [
      {
        "values": [
          {
            "text": "Abby is <? $days ?> months old."
          },
          {
            "text": "The wonderful and beautiful Abbygale is <? $days ?>months old."
          },
          {
            "text": "The incredibly smart and savvy Abby is <? $days ?> months old."
          }
        ],
        "response_type": "text",
        "selection_policy": "sequential"
      }
    ]
  }
}

【问题讨论】:

    标签: json watson-conversation


    【解决方案1】:

    为了更精确,将您的代码天数更改为数月。 请注意,SpEL 使用的是 Java Date,因此 2018 年 7 月 27 日是 new Date(118,6,27)。 所以更改后的解决方案将是:

    {
      "context": {
        "months": "<? (new Date().getYear() * 12 + new Date().getMonth()) - (new Date(118,6,27).getYear() * 12 + new Date(118,6,27).getMonth()) + 1 ?>"
      },
      "output": {
        "generic": [
          {
            "values": [
              {
                "text": "Abby is <? $months ?> months old."
              },
              {
                "text": "The wonderful and beautiful Abbygale is <? $months ?>months old."
              },
              {
                "text": "The incredibly smart and savvy Abby is <? $months ?> months old."
              }
            ],
            "response_type": "text",
            "selection_policy": "sequential"
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2017-05-26
      • 2017-08-08
      相关资源
      最近更新 更多