# How to pass in \`current\_unix\_time\` as a value for elasticsearch query?

**URL:** <https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [April 30, 2023, 10:44pm UTC](https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385 "2023-04-30T22:44:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningelastic](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@learningelastic](https://discuss.elastic.co/u/learningelastic)\
**Post date:** [April 30, 2023, 10:44pm UTC](https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385/1 "2023-04-30T22:44:27Z")

</div>

My ultimate goal is to create a Kibana visualization that shows "How many days have passed since [today]". I was told I might be able to create an elasticsearch query that passes in a `current_unix_time` or `(new Date()).getTime()` or `NOW()` or similar, then use Vega to draw a visualization for my dashboard.

I understand that I can NOT put in a unix time as part of my painless `script.source` query (because if the distributed computing nature of an elastic cluster), but then someone told me to try to supply a current time as a `script.params`. But I don't know the right syntax. Here's an example of what I tried:

```auto
GET myindex/_search
{
  "runtime_mappings": {
    "new_field": {
      "type": "keyword",
      "script": {
        "source": "emit(params['now'])",
        "params": {
          "now": "(new Date()).getTime()"
          }
      }
    }
  },
    "aggs":{
    "new_field": {
      "terms": {
      "field": "new_field"
      }
    }
  }

```

But the run time field just gives me the literal string `(new Date()).getTime()` and not the actual time value. I've tried other things like changing the runtime field type, other possible functions/methods that I think might give me a time, but I still can't figure it out.

What am I doing wrong? What's the best way to work with a `current_time` in a painless script? Or to plot visualizations that can do something like `DATE_DIFF( docs['projectDueDate'].date , NOW() )`?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 30, 2023, 11:17pm UTC](https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385/2 "2023-04-30T23:17:31Z")

</div>

Hi @learningelastic

Let's back up a bit

> [@learningelastic](#):
>
> "How many days have passed since [today]"

I am confused what you are actually trying to accomplish....

Are you trying to calculate the number of days from Today / Now with respect to a date field in the document?

> [@learningelastic](#):
>
> I understand that I can NOT put in a unix time as part of my painless

I am not sure where that understanding comes from...

Perhaps you are looking to do something like this

> [@Calculate elapsed time in Kibana?](https://discuss.elastic.co/t/calculate-elapsed-time-in-kibana/316575/2):
>
> Hi @TXBigDawg1836 Welcome to the community Yup add a runtime field to the Data View here is the code long datenow = new Date().getTime(); long datewas = doc['@timestamp'].value.getMillis(); emit (datenow - datewas); Create / Set the Field Chose your format.... In Discover Now Table...

---

<div class="post-metadata">

**Author:** ![learningelastic](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@learningelastic](https://discuss.elastic.co/u/learningelastic)\
**Post date:** [May 1, 2023, 5:30pm UTC](https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385/3 "2023-05-01T17:30:56Z")

</div>

Perfect! Based on that answer, I made this example for myself, which shows number of days a project has been overdue:

```auto
PUT runtime_field
{
  "mappings": {
    "properties": {
      "dueDate": {
        "type": "date",
        "format": "yyyy-MM-dd"
      }
    }
  }
}

POST runtime_field/_doc
{
  "dueDate": "2023-04-01"
}

GET runtime_field/_search
{
  "runtime_mappings": {
    "daysOverdue": {
      "type": "long",
      "script": {
        "source": """
          long current = (new Date()).getTime();
          long dueDate = doc['dueDate'].value.getMillis();
          long days = Math.round((current - dueDate)/(1000*3600*24));
          emit(days)
          """
      }
    }
  },
    "aggs":{
    "daysOverdue": {
      "terms": {
      "field": "daysOverdue"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 1, 2023, 5:33pm UTC](https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385/4 "2023-05-01T17:33:54Z")

</div>

You can just add that runtime mapping to the index mapping and then it will always be there...

Although if you only need it when you do that search the way you're doing it is perhaps a bit more efficient.

Either way, it's good and thanks for posting your answer. It may help someone else!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 29, 2023, 5:34pm UTC](https://discuss.elastic.co/t/how-to-pass-in-current-unix-time-as-a-value-for-elasticsearch-query/331385/5 "2023-05-29T17:34:31Z")

</div>

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