简体   繁体   中英

Calculating age in JSON IBM Watson Assistant

I'm having some difficulty performing a simple date calculation in Watson's JSON editor. I have a known value which is a date: July 27, 2018, and what I need to do is calculate and output an age in months and days based on the current date. I've tried to figure this out based on the examples given in Expression Language Methods, but I've been unsuccessful.

What I need to do is replace the (new Date(2019, 2, 13)).getTime() to something like today() so that in can calculate the age in month in real-time whenever someone asks for the age, I've tried replacing it with Today(), but it causes an error...

{
  "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"
      }
    ]
  }
}

Change in your code days to months in order to be more precise. Note that SpEL is using Java Date , so 27 July, 2018 is new Date(118,6,27) . So the changed solution will be:

{
  "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"
      }
    ]
  }
}

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