# Painless dates in update script

**URL:** <https://discuss.elastic.co/t/painless-dates-in-update-script/84849>\
**Category:** Elasticsearch\
**Created:** [May 7, 2017, 1:48pm UTC](https://discuss.elastic.co/t/painless-dates-in-update-script/84849 "2017-05-07T13:48:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Markhasin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_markhasin/32/14187_2.png) [@Dan\_Markhasin](https://discuss.elastic.co/u/Dan_Markhasin)\
**Post date:** [May 7, 2017, 1:48pm UTC](https://discuss.elastic.co/t/painless-dates-in-update-script/84849/1 "2017-05-07T13:48:21Z")

</div>

I'm a bit struggling with creating an update script in painless that would compare two date fields.

Here is what I have so far (the condition is bogus just for syntax testing):

> {  
> "script" : {  
> "inline": "if (Instant.parse(ctx.\_source.logtime).isBefore(Instant.parse(ctx.\_source.logtime))) { ctx.op = "none" }",  
> "lang":"painless"  
> }  
> }

And this works, except that it expects the field to be in a very particular format. The same script fails on other date fields, for example:

> "script": "if (Instant.parse(ctx.\_source.finishtime).isBefore(Instant.parse(ctx.\_source.logtime))) { ctx.op = "none" }",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "date\_time\_parse\_exception",  
> "reason": "date\_time\_parse\_exception: Text '2017-04-27T10:32:16' could not be parsed at index 19"

Both logtime and finishtime are date fields, but one is of the format yyyy-MM-dd'T'HH:mm:ss while the other is yyyy-MM-dd'T'HH:mm:ss.SSSZZ.

Is there a generic approach for working with dates in a painless update script that would handle both types of date formats?

---

<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 4, 2017, 1:48pm UTC](https://discuss.elastic.co/t/painless-dates-in-update-script/84849/2 "2017-06-04T13:48:56Z")

</div>

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