# Comparing timestamps in painless

**URL:** https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858
**Category:** Elasticsearch
**Created:** [January 17, 2018, 10:07am UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858 "2018-01-17T10:07:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)
#### Post date: [January 17, 2018, 10:07am UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858/1 "2018-01-17T10:07:21Z")

</div>

How do I check if a payload timestamp (\_source['@timestamp']) to scheduled time plus 5 minutes?

```
def schedTimestamp=Instant.ofEpochMilli(ctx.trigger.scheduled_time.getMillis()).plus(Duration.ofMinutes(+5);
def payloadTimestamp=ctx.payload.hits.hits[0]._source['@timestamp'];
return schedTimestamp.isBefore(payloadTimestamp);

```

I'm trying to make use of the mechanism in a more complicated script transform but I can't seem to get an Instant of payloadTimestamp.

---

<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 17, 2018, 12:02pm UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858/2 "2018-01-17T12:02:01Z")

</div>

Hey,

the initial idea is correct. However `ctx.payload.hits.hits[0]._source['@timestamp']` is not of type datetime but just a string, thus the comparison fails. You need to convert that one also into an Instant. You can try to use `Instant.parse()` for that.

--Alex

---

<div class="post-metadata">

### Author: ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)
#### Post date: [January 17, 2018, 12:12pm UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858/3 "2018-01-17T12:12:31Z")

</div>

Thanks for the swift reply.  
My date is in the format  
2018-01-17T11:10:42.217+00:00  
As opposed to the expected  
2018-01-17T11:10:42.217Z

I get the following error:  
{  
"type": "date\_time\_parse\_exception",  
"reason": "Text '2018-01-17T11:10:42.217Z+00:00' could not be parsed, unparsed text found at index 24"  
}

I've had a look at the java.time doc for Instant and there doesn't seem to be a parse method that allows for a dateformatter.  
Would you be able to make a suggestion?

---

<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 18, 2018, 8:50am UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858/4 "2018-01-18T08:50:18Z")

</div>

How about something like

```auto
def formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
def x = LocalDateTime.parse(ctx.payload.timestamp, formatter);

```

--Alex

---

<div class="post-metadata">

### Author: ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)
#### Post date: [January 18, 2018, 12:11pm UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858/5 "2018-01-18T12:11:52Z")

</div>

Thanks again for the help.

I gave that a go but despite whilst it successfully parsed the string it seems to just be culling the zoning entirely.  
Using the same DateTimeFormatter constant I used the following:

`Instant.from( DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse( ctx.payload.timestamp ) );`

and that produces an Instant with the offset.

Thanks for the support!

---

<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: [February 15, 2018, 12:11pm UTC](https://discuss.elastic.co/t/comparing-timestamps-in-painless/115858/6 "2018-02-15T12:11:56Z")

</div>

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