# How to get date difference in weeks between today and date\_timestamp field

**URL:** <https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378>\
**Category:** Kibana\
**Created:** [March 5, 2021, 2:28pm UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378 "2021-03-05T14:28:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Rayabarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakesh_rayabarapu/32/79132_2.png) [@Rakesh\_Rayabarapu](https://discuss.elastic.co/u/Rakesh_Rayabarapu)\
**Post date:** [March 5, 2021, 2:28pm UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378/1 "2021-03-05T14:28:11Z")

</div>

Hello Team,

could any one help me to write scripted field to get date difference in weeks between current datetime and doc['timestamp'].  
to get current date time used datetime.new date().getTime()

Used this script.  
(new Date().getTime()-doc['timestamp'].value)/7;

please give any other way if we have!

Refer error:  
{  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"(new Date().getTime()-doc['timestamp'].value)/7",  
" ^---- HERE"  
],  
"script": "(new Date().getTime()-doc['timestamp'].value)/7",  
"lang": "painless",  
"position": {  
"offset": 38,  
"start": 0,  
"end": 47  
}  
}  
],  
"type": "search\_phase\_execution\_exception",  
"reason": "all shards failed",  
"phase": "query",  
"grouped": true,  
"failed\_shards": [  
{  
"shard": 0,  
"index": "kibana\_sample\_data\_logs",  
"node": "8mBwNeFOTrucFuoSsAwrog",  
"reason": {  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"(new Date().getTime()-doc['timestamp'].value)/7",  
" ^---- HERE"  
],  
"script": "(new Date().getTime()-doc['timestamp'].value)/7",  
"lang": "painless",  
"position": {  
"offset": 38,  
"start": 0,  
"end": 47  
},  
"caused\_by": {  
"type": "class\_cast\_exception",  
"reason": "Cannot apply [-] operation to types [java.lang.Long] and [org.elasticsearch.script.JodaCompatibleZonedDateTime]."  
}  
}  
}  
]  
}

Thanks in advance.

Rakesh

---

<div class="post-metadata">

**Author:** ![Rakesh\_Rayabarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakesh_rayabarapu/32/79132_2.png) [@Rakesh\_Rayabarapu](https://discuss.elastic.co/u/Rakesh_Rayabarapu)\
**Post date:** [March 5, 2021, 2:31pm UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378/2 "2021-03-05T14:31:46Z")

</div>

Used this one too:

(datetime.new Date().getTime()-doc['timestamp'].value)/7

error:  
],  
"script": "(datetime.new Date().getTime()-doc['timestamp'].value)/7",  
"lang": "painless",  
"position": {  
"offset": 14,  
"start": 0,  
"end": 39  
},  
"caused\_by": {  
"type": "illegal\_argument\_exception",  
"reason": "invalid sequence of tokens near ['Date'].",  
"caused\_by": {  
"type": "no\_viable\_alt\_exception",  
"reason": null  
}  
}  
}  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [March 5, 2021, 3:43pm UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378/3 "2021-03-05T15:43:59Z")

</div>

I think your question is answered in _very complete detail_ by the Painless docs about using datetime:

> **[Using Datetime in Painless | Painless Scripting Language \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-datetime.html#_age_of_a_message_script_field_example)**

> **[Using Datetime in Painless | Painless Scripting Language \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-datetime.html#_datetime_input_from_an_indexed_document)**

> **[Using Datetime in Painless | Painless Scripting Language \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-datetime.html#_datetime_now)**

---

<div class="post-metadata">

**Author:** ![nickofthyme](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickofthyme/32/60032_2.png) [@nickofthyme](https://discuss.elastic.co/u/nickofthyme)\
**Post date:** [March 5, 2021, 4:38pm UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378/4 "2021-03-05T16:38:47Z")

</div>

Hey Rakesh,

Your script is erroring because `doc['timestamp'].value` is not a `long` but a [`ZonedDateTime`](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-shared-java-time.html#painless-api-reference-shared-ZonedDateTime) see the available methods to convert to `long`

A simple example could be...

```java
ZonedDateTime now = ZonedDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.of('Z'));
double days = doc['timestamp'].value.until(now, ChronoUnit.DAYS);
double daysInWeek = 7;
days/daysInWeek;

```

> You must assign to `double` to avoid rounded values.

In addition to the links @wylie provided this is also a good reference [Shared API for package java.time | Painless Scripting Language [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-shared-java-time.html)

---

<div class="post-metadata">

**Author:** ![Rakesh\_Rayabarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakesh_rayabarapu/32/79132_2.png) [@Rakesh\_Rayabarapu](https://discuss.elastic.co/u/Rakesh_Rayabarapu)\
**Post date:** [March 9, 2021, 7:36am UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378/5 "2021-03-09T07:36:56Z")

</div>

Thanks Nick,

This is exactly match with my requirement.

---

<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:** [April 6, 2021, 7:37am UTC](https://discuss.elastic.co/t/how-to-get-date-difference-in-weeks-between-today-and-date-timestamp-field/266378/6 "2021-04-06T07:37:42Z")

</div>

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