# Calculate time with "ctx.execution\_time"

**URL:** <https://discuss.elastic.co/t/calculate-time-with-ctx-execution-time/87332>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [May 27, 2017, 9:19am UTC](https://discuss.elastic.co/t/calculate-time-with-ctx-execution-time/87332 "2017-05-27T09:19:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![foxfire881](https://avatars.discourse-cdn.com/v4/letter/f/ed655f/32.png) [@foxfire881](https://discuss.elastic.co/u/foxfire881)\
**Post date:** [May 27, 2017, 9:19am UTC](https://discuss.elastic.co/t/calculate-time-with-ctx-execution-time/87332/1 "2017-05-27T09:19:28Z")

</div>

Hi everyone，I want to subtract 7 days from "ctx.execution\_time" and transform it to a string, so my "transform" is as this:

```
 "transform": {
    "script": {
       "inline": "ctx.metadata.t1 = ctx.execution_time.minusDays(7).toString('yyyy-MM-dd HH:mm:ss.SSS')",
       "lang": "painless"
     }
  }

```

when i run the watch, it gives me the error:

```
  "transform": {
       "type": "script",
       "status": "failure",
       "reason": "ScriptException[runtime error]; nested: IllegalArgumentException[Unable to find dynamic method [minusDays] with [1] arguments for class [org.joda.time.DateTime].]; "
     }

```

but I check joda API with Java, the "minusDays" method exists in DateTime class. So, how should i write the transform? Thanks.

---

<div class="post-metadata">

**Author:** ![foxfire881](https://avatars.discourse-cdn.com/v4/letter/f/ed655f/32.png) [@foxfire881](https://discuss.elastic.co/u/foxfire881)\
**Post date:** [May 27, 2017, 11:28am UTC](https://discuss.elastic.co/t/calculate-time-with-ctx-execution-time/87332/2 "2017-05-27T11:28:54Z")

</div>

I find the reason, "ctx.execution\_time" is a class of **org.joda.time.DateTime** for Java, but my watcher is wrotten with X-Pack 5.4, the default script language of X-Pack 5.x is "painless" but not Java, and, there is no DateTime class in painless!!!!

so, how can I calcuate time with **org.joda.time.DateTime** of Java in painless?? I feel painless script is not pain less, but pain more 😞

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 29, 2017, 11:05am UTC](https://discuss.elastic.co/t/calculate-time-with-ctx-execution-time/87332/3 "2017-05-29T11:05:37Z")

</div>

Hey,

try something like

```auto
return Instant.ofEpochMilli(ctx.execution_time.getMillis()).minus(7, ChronoUnit.DAYS)

```

in your transform.

--Alex

---

<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:** [June 26, 2017, 11:05am UTC](https://discuss.elastic.co/t/calculate-time-with-ctx-execution-time/87332/4 "2017-06-26T11:05:37Z")

</div>

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