0

I developing for Bixby and am using DateTimeExpression in training but am struggling with how I turn this into an ISO date string for a call to an external API.

rogerkibbe
  • 358
  • 2
  • 10

1 Answers1

0

Bixby's DateTimeExpression is powerful and not only captures a date but also intervals e.g. "next March" or "between March 1st and 15th". When you use DateTimeExpression, Bixby will parse the input and set either a Date, DateTime, DateInterval or DateTimeInterval.

To parse the values out of DateTimeExpression, you should check which field was populated. Good example code for this is from the Earthquake example in the documentation. For the earthquakes example, you would ask a question like "Find earthquakes that occurred on March 12, 1963" or "Find earthquakes that occurred in March, 1963". Here is sample code (from the docs)

module.exports = function findEarthquakes(
  where, dateTimeExpression, minMagnitude, classification
) {
  var whenStart;
  var whenEnd;

  if (dateTimeExpression.date) {
    whenStart = dates.ZonedDateTime.fromDate(dateTimeExpression.date);
    whenEnd = whenStart.withHour(23).withMinute(59).withSecond(59);
  }
  else if (dateTimeExpression.dateInterval) {
    whenStart = dates.ZonedDateTime.of(
      dateTimeExpression.dateInterval.start.year,
      dateTimeExpression.dateInterval.start.month,
      dateTimeExpression.dateInterval.start.day);
    whenEnd = dates.ZonedDateTime.of(
      dateTimeExpression.dateInterval.end.year,
      dateTimeExpression.dateInterval.end.month,
      dateTimeExpression.dateInterval.end.day,
      23, 59, 59);
  }
  else if (dateTimeExpression.dateTimeInterval) {
    whenStart = dates.ZonedDateTime.of(
      dateTimeExpression.dateTimeInterval.start.year,
      dateTimeExpression.dateTimeInterval.start.month,
      dateTimeExpression.dateTimeInterval.start.day,
      dateTimeExpression.dateTimeInterval.start.hour,
      dateTimeExpression.dateTimeInterval.start.minute,
      dateTimeExpression.dateTimeInterval.start.second);
    whenEnd = dates.ZonedDateTime.of(
      dateTimeExpression.dateTimeInterval.end.year,
      dateTimeExpression.dateTimeInterval.end.month,
      dateTimeExpression.dateTimeInterval.end.day,
      dateTimeExpression.dateTimeInterval.end.hour,
      dateTimeExpression.dateTimeInterval.end.minute,
      dateTimeExpression.dateTimeInterval.end.second);
  }

  var start = whenStart.toIsoString();
  var end = whenEnd.toIsoString();

  // code continues...
}

In the example above, start and end are ISO date strings. If the user specified only a single date, end is set as the end of that day.

rogerkibbe
  • 358
  • 2
  • 10