# Painless script for date to minutes calculation

**URL:** https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252
**Category:** Kibana
**Created:** [July 30, 2020, 4:15pm UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252 "2020-07-30T16:15:25Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)
#### Post date: [July 30, 2020, 4:15pm UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/1 "2020-07-30T16:15:25Z")

</div>

Hi All,

We are ingesting data using logstash http\_poller, there is a date field which need to be subtracted with a default value (1970.01.01 00:00:00)

eg: result.timeworked : 1970.01.01 00:00:40

this field need to be subtracted with the default value(1970.01.01 00:00:00) , get the result as 40.

How do i achieve this? any advice on this please.

Thanks  
Gautham

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [July 31, 2020, 2:03pm UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/2 "2020-07-31T14:03:07Z")

</div>

Hi

Have a look here:

[https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-datetime.html#\_datetime\_difference\_elapsed\_time](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-datetime.html#_datetime_difference_elapsed_time)

There's an example you could use for that:

```
ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));
ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 13, 22, 15, 35, 0, ZoneId.of('Z'));
long differenceInMillis = ChronoUnit.MILLIS.between(zdt1, zdt2);

```

this is returning milliseconds, but it shouldn't be too difficult to calculate seconds for your case  
Best,  
Matthias

---

<div class="post-metadata">

### Author: ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)
#### Post date: [August 2, 2020, 9:06am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/3 "2020-08-02T09:06:45Z")

</div>

Thanks for the response @matw , just wanted to understand the way to add the default field using logstash, is there a way to do that.  
I dont have any field that contains the value 1970.01.01 00:00:00.  
FIrst i need a way to add this value into elasticsearch, how do i do that, the rest api which i'm using to ingest data doesnot have this value in it.

Thanks  
Gautham

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [August 3, 2020, 5:50am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/4 "2020-08-03T05:50:16Z")

</div>

You just need the seconds for this field, right? so why don't you use a numeric field in ES to store the value?  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html)  
Best,  
Matthias

---

<div class="post-metadata">

### Author: ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)
#### Post date: [August 3, 2020, 6:34am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/5 "2020-08-03T06:34:29Z")

</div>

@matw i actually need to subtract two fields and get the time difference .

**Field One** i have it in elasticsearch, lets assume that field as "time\_worked" and this field has a date value 1970.01.01 00:00:40

time\_worked : 1970.01.01 00:00:40

**Field Two** i dont have it in elasticsearch and value of this field is a static date field, will have a value 1970.01.01 00:00:00

to use a painless script and subtract these two values i need to add the **FIeld Two** into elasticsearch. How do i do that?

Any advice please.

Thanks  
Gautham

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [August 3, 2020, 11:56am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/6 "2020-08-03T11:56:57Z")

</div>

Since field two is a static field, so it has always the same value, right?

---

<div class="post-metadata">

### Author: ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)
#### Post date: [August 3, 2020, 2:03pm UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/7 "2020-08-03T14:03:30Z")

</div>

Yeah you are rite. It will always have the same value and it will be a date field.

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [August 4, 2020, 8:00am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/8 "2020-08-04T08:00:38Z")

</div>

If it doesn't change, why no calculating the value in log stash and storing it in a numeric field in Elasticsearch? this is the best option in this case. You can do this with a ruby filter in Logstash, here's an example that's a similar use case:

> [@Get Difference between two date field with ruby filter](https://discuss.elastic.co/t/get-difference-between-two-date-field-with-ruby-filter/97100):
>
> Hi All I have been trying the ruby code to get the difference between 2 dates Start Date : 2017-06-13T01:17:07.000Z End Date : 2017-07-13T01:17:10.000Z I need difference between these 2 dates. I have dried this but i get ruby exception error ruby { init =\> "require 'time'" code =\> " start = Time.iso8601(event['Start Date'].to\_s).to\_i; end = Time.iso8601(event['End Date'].to\_s).to\_i; event['timediff'] = start - end; " add\_tag =\> [ "calculat…

Best,  
Matthias

---

<div class="post-metadata">

### Author: ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)
#### Post date: [August 5, 2020, 9:45am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/9 "2020-08-05T09:45:35Z")

</div>

@matw To go as per the example mentioned i dont have a second time as a field, this is a static value which i need to insert it, can i directly subtract the first date with the static date and year  
StartDate - 01-01-1970 00:00:00

I dont think so this will work.

Thanks  
Gautham

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [August 6, 2020, 6:43am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/10 "2020-08-06T06:43:09Z")

</div>

Yes, but you can construct a static date in the ruby script, there are several ways to do this  
[https://ruby-doc.org/stdlib-2.5.0/libdoc/time/rdoc/Time.html](https://ruby-doc.org/stdlib-2.5.0/libdoc/time/rdoc/Time.html)  
and then use it for the subtraction  
Best,  
Matthias

---

<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: [September 3, 2020, 6:43am UTC](https://discuss.elastic.co/t/painless-script-for-date-to-minutes-calculation/243252/11 "2020-09-03T06:43:09Z")

</div>

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