Showcasing Date as yesterday, this week, this month in Kibana Dashboard control options

Hi Team,
i have a date field which showcases date only as per below snap.


this date is basically being extracted from one of the available field invoice_date. Now my requirement is that suppose today is 20 Oct and i have data for 19th october, then 19th date should appear as yesterday.
Also, another way which i was thinking is to replicate invoice_date field and use relative time, will it help?

The easiest way is to use the Relative Date format in the Data view for your date field

But if you want full control of the texts you can create a new runtime field that computes the date distance and then start coding your checks.

For this dataset with just a date field:

PUT discuss-345435
{
  "mappings": {
    "properties": {
      "date": { "type": "date"}
    }
  }
}

POST discuss-345435/_bulk
{ "index": {}}
{ "date": "2023-10-20"}
{ "index": {}}
{ "date": "2023-10-21"}
{ "index": {}}
{ "date": "2023-10-22"}
{ "index": {}}
{ "date": "2023-10-23"}
{ "index": {}}
{ "date": "2023-10-24"}
{ "index": {}}
{ "date": "2023-10-25"}
{ "index": {}}
{ "date": "2023-10-26"}
{ "index": {}}
{ "date": "2023-10-27"}
{ "index": {}}
{ "date": "2023-10-28"}

I'll show only for Yesterday (the easy one) and leave the rest for you:

Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());
Instant Startdate = Instant.ofEpochMilli(doc['date'].value.getMillis());
long days_ago = ChronoUnit.DAYS.between(Startdate, Currentdate);

if (days_ago == 1){
  emit('Yesterday');
} else {
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  emit(doc['date'].value.format(dtf));
}

And that new field shows like this in Discover

This is a great resource to work with dates in Painless language

Hi Jsanz. thanks for your reply. this helps :slight_smile:

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.