简体   繁体   中英

How to calculate duration of time from 2 datetimes in IBM Watson Assistant

Can't subtract a datetime from another datetime in IBM Watson Assistant to find the duration between the 2 datetimes.

I have defined 2 context variable in a dialog in Watson Assistant to hold dates:

variable = $base_date  value = "2019-07-01 03:00:00"
variable = $current_date  value = "<? now() ?>"

Those values are strings and I can provide them in a response. However, I can't figure out how to convert them to datetime so that I can then subtract $current_date from $base_date to respond with a duration such as duration = aa Days, bb Hours, cc Minutes, dd Seconds . I can't figure out how to convert them to milliseconds from Jan 1, 1970 so that I can then do the math between the two dates.

Context variables:

$base_date     "2019-07-01 03:00:00"
$current_date  "<? now() ?>"

Response:

Base date = <? $base_date ?>
Current date = <? $current_date ?>

...above works fine to show user the dates in question as follows:

Base date = 2019-07-01 03:00:00
Current date = 2019-05-21 12:24:26

However, this does not work...

Duration = <? $base_date - $current_date ?>

Here is the error I receive, obviously telling me can't subtract 2 strings. I just can't figure out how to convert to a date time or number I can manipulate....

Dialog node error

Error when updating output with output of dialog node id [Welcome]. Node output is [{"text":{"values":["Base date = \\nCurrent date = ... Duration = "],"selection_policy":"sequential"}}] SpEL evaluation error: Expression [ $base_date - $current_date ] converted to [ context['base_date'] - context['current_date'] ] at position 0: EL1030E: The operator 'SUBTRACT' is not supported between objects of type 'String' and 'String'

data_henrik's suggestion to delegate this logic to a Cloud Functions action or in a logic layer outside of Watson Assistant is a sound suggestion. You have to work really hard to do this in Watson Assistant, but it can be done.

Through Integer and substring you can get the individual date parts.

  "context": {
    "base_year": "<? T(Integer).parseInt($base_date.substring(0,4)) ?>",
  }

You can use the Date constructor from Java to ultimately get the milliseconds value. I provide base_year as a simple example. For base_time I have added indentation for readability.

  "context": {
    "base_year": "<? T(Integer).parseInt($base_date.substring(0,4)) ?>",
    "base_time": "<? new Date(
        T(Integer).parseInt($base_date.substring(0,4)),
        T(Integer).parseInt($base_date.substring(5,7)),
        T(Integer).parseInt($base_date.substring(8,10)),
        T(Integer).parseInt($base_date.substring(11,13)),
        0,0).getTime() ?>",
    "current_time": "<? new Date(2019,05,24,22,14,12).getTime() ?>"
  }

Then you can respond with the following text:

Duration in days = <? ($base_time - $current_time)/(1000*60*60*24) ?>

I would recommend the IBM Watson Assistent documentation on working with date and time values , including computations. The expression language has several methods to add or subtract values. There are also examples on how to reformat date/time values to serve as input for further computations.

The section on working with time spans might be what you are searching for. You can use operations for java.util.Date . That class has operations to turn the data string into a long with milliseconds since 1970 .

For complex conversions or computations you may want to delegate this to a Cloud Functions action. See this section on making programmatic calls .

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