在JSON IBM Watson Assistant中计算年龄

问题描述 投票:1回答:1

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

我需要做的是将(新的日期(2019年,2年级,13年))。getTime()替换为类似今天()的东西,这样当有人要求年龄时,我可以实时计算月份的年龄,我试过用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个回答
0
投票

将代码天数更改为几个月,以便更精确。请注意,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"
      }
    ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.