# Kibana: Scripted Field: Dynamic date (now - 15d / now - 30d) not working

**URL:** <https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781>\
**Category:** Kibana\
**Created:** [April 10, 2021, 6:07pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781 "2021-04-10T18:07:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nshari.24](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@nshari.24](https://discuss.elastic.co/u/nshari.24)\
**Post date:** [April 10, 2021, 6:07pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781/1 "2021-04-10T18:07:23Z")

</div>

I have a scripted field of the format "Date". When I try using this field in Kibana visualization and apply a filter like "date \> now-30d",  
I am getting an error:

```auto
    caused_by": {
                "type": "date_time_parse_exception",
                "reason": "Text 'now-15d' could not be parsed at index 0"
              }

```

The field works fine when used for Date Histogram. Please suggest what is wrong and what can be done to fix this.

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [April 12, 2021, 5:37am UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781/2 "2021-04-12T05:37:44Z")

</div>

Can you share your mappings?

---

<div class="post-metadata">

**Author:** ![nshari.24](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@nshari.24](https://discuss.elastic.co/u/nshari.24)\
**Post date:** [April 12, 2021, 7:13am UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781/3 "2021-04-12T07:13:03Z")

</div>

```auto
    "results" : {
                "completed_on" : 1617936964,
                "created_time" : "2021-04-05 15:35:58",
                "include_all" : false,
                "url" : "String",
                "created_on" : 1617636958,
                "name" : "test",
    }

```

I am trying to convert the time string to DateTime and the script is

```auto
    if(doc.containsKey('results.created_time.keyword') && doc['results.created_time.keyword'].size() != 0) { return Instant.ofEpochSecond(doc['results.created_time.keyword'].value).atZone(ZoneId.systemDefault()); } else { return Instant.ofEpochSecond(0).atZone(ZoneId.systemDefault()); }

```

I have also tried using DateFormatter and return millis, but getting a different error "toInstant" method is not found (as ES is trying to convert the output of this script to Instant)

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [April 13, 2021, 1:27am UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781/4 "2021-04-13T01:27:52Z")

</div>

So your field is text and not date.  
First, convert it successfully to date.  
You can use [painless lab](https://www.elastic.co/guide/en/kibana/master/painlesslab.html) for this.

---

<div class="post-metadata">

**Author:** ![nshari.24](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@nshari.24](https://discuss.elastic.co/u/nshari.24)\
**Post date:** [April 13, 2021, 11:11am UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781/5 "2021-04-13T11:11:47Z")

</div>

As mentioned, am using a scripted field for the date part. But I am not able to apply range filters to this scripted Field.

However, I can use this field in the Date Histogram and it works. When I try applying the range filter I get the above error:

> [@nshari.24](#):
>
> ```auto
> caused_by": {
> "type": "date_time_parse_exception",
> "reason": "Text 'now-15d' could not be parsed at index 0"
> }
> 
> ```

Want to know if what causes date Histogram to work but not range filters.

---

<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 11, 2021, 11:12am UTC](https://discuss.elastic.co/t/kibana-scripted-field-dynamic-date-now-15d-now-30d-not-working/269781/6 "2021-05-11T11:12:43Z")

</div>

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