# How to increase a date by e.g. 1 day using Painless script?

**URL:** <https://discuss.elastic.co/t/how-to-increase-a-date-by-e-g-1-day-using-painless-script/216873>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [January 28, 2020, 4:07pm UTC](https://discuss.elastic.co/t/how-to-increase-a-date-by-e-g-1-day-using-painless-script/216873 "2020-01-28T16:07:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohanP](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@JohanP](https://discuss.elastic.co/u/JohanP)\
**Post date:** [January 28, 2020, 4:07pm UTC](https://discuss.elastic.co/t/how-to-increase-a-date-by-e-g-1-day-using-painless-script/216873/1 "2020-01-28T16:07:34Z")

</div>

Just starting to use Painless as excercise before certification.

Am wondering how to attack the problem of updating/increasing a date field by using a Painless script?  
I know how to for example increase a simple counter:

```
POST test/_update/1
{
  "script":
  {
    "source": "ctx._source.counter += params.count",
    "lang": "painless",
    "params": {
      "count": 1
    }
  }
}

```

, but don´t really get it when it comes to date formats.

See extract from my sample mapping below, how would I go about to update any of those by 1 day?

```
    "revision_date" : {
      "type" : "date"
    },
    "revision_date_epoch" : {
      "type" : "date",
      "format" : "epoch_second"
    },
    "revision_date_epochm" : {
      "type" : "date",
      "format" : "epoch_millis"
    }

```

Been trying according to posts that I´ve found but hope to get the strategy explained here to get going.

Thx

---

<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:** [January 29, 2020, 3:20pm UTC](https://discuss.elastic.co/t/how-to-increase-a-date-by-e-g-1-day-using-painless-script/216873/2 "2020-01-29T15:20:46Z")

</div>

`ctx._source` is just a hashmap of strings, so it depends on the string representation of the date.

for epochs, you could do something like `Instant.ofEpochMilli(ctx._source.revision_date_epochm).plus(Duration.ofDays(1))` - on top of my head and untested...

for epoch seconds there is `Instant.ofEpochSecond()`, for a string based date you would need to use a dateformatter or hope that something like `ZonedDate.parse` works out of the box.

---

<div class="post-metadata">

**Author:** ![JohanP](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@JohanP](https://discuss.elastic.co/u/JohanP)\
**Post date:** [February 2, 2020, 7:24pm UTC](https://discuss.elastic.co/t/how-to-increase-a-date-by-e-g-1-day-using-painless-script/216873/3 "2020-02-02T19:24:34Z")

</div>

Hi and thx for answer, will explore your suggestions.  
\Johan

---

<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:** [March 1, 2020, 7:26pm UTC](https://discuss.elastic.co/t/how-to-increase-a-date-by-e-g-1-day-using-painless-script/216873/4 "2020-03-01T19:26:10Z")

</div>

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