Can I get the datepicker parameters to use in a KQL query in Kibana

My data has multiple date fields so I would like to get the start and end dates from the datepicker to use in a KQL query to further filter my data in a Kibana visualisation.

So let's say I have a document like this:

{
  admissionDate: "date",
  dischargeDate: "date",
  ...
}

My index pattern uses admissionDate as the time field, so the datepicker will filter all patients admitted between the start and end date. But I want to filter patients who have also been discharged before the end date.

I can do this in Vega using the timefilter like this

%timefilter%: max

Is there a way to do this in KQL, something like this?

dischargeDate < "%timefilter%: max"

No, this is not currently supported. You can use regular datemath in KQL, so for example you can do date > now-7d/d to find all documents within the last 7 days rounded to midnight.

Here is the issue: [data.search.aggs] Date range aggregation should support time ranges relative to timepicker · Issue #76433 · elastic/kibana · GitHub

Thanks for the reply, I'll keep an eye on the github issue.

I went another way of getting around part of the problem in case anyone is interested.

I created aliases for my index, eg:

"aliases": {
  "my-index-by-admission": {},
  "my-index-by-discharge": {}
}

Then I created an index pattern for each alias, and each one has a different time field. So I can use the same index and filter by a different time field by selecting a different index pattern.

It doesn't get around the problem of using the same datepicker for 2 dates, but it solves some other problems.

If anyone has any better ideas, feel free to drop them here.