# Convert timestamp to date - painless scripting

**URL:** <https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025>\
**Category:** Elasticsearch\
**Created:** [October 18, 2018, 2:25pm UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025 "2018-10-18T14:25:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeWalker](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@CodeWalker](https://discuss.elastic.co/u/CodeWalker)\
**Post date:** [October 18, 2018, 2:25pm UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/1 "2018-10-18T14:25:00Z")

</div>

Hi,  
How to convert timestamp to date in painless script ?

---

<div class="post-metadata">

**Author:** ![Court](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/court/32/6640_2.png) [@Court](https://discuss.elastic.co/u/Court)\
**Post date:** [October 18, 2018, 2:47pm UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/2 "2018-10-18T14:47:22Z")

</div>

I moved this post from the Kibana forum because painless is an Elasticsearch driven feature.

---

<div class="post-metadata">

**Author:** ![CodeWalker](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@CodeWalker](https://discuss.elastic.co/u/CodeWalker)\
**Post date:** [October 19, 2018, 7:34am UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/3 "2018-10-19T07:34:29Z")

</div>

Plop

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 22, 2018, 3:50pm UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/4 "2018-10-22T15:50:44Z")

</div>

What type of painless script? Can you give an example request with a script, showing the field or value you want to convert?

---

<div class="post-metadata">

**Author:** ![CodeWalker](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@CodeWalker](https://discuss.elastic.co/u/CodeWalker)\
**Post date:** [October 29, 2018, 10:20am UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/5 "2018-10-29T10:20:34Z")

</div>

Hi,  
I have a timestamp like : 1488350778.  
I want to get the month, year, semester, trimester from this timestamp.  
How to do that in painless script ? For the type, I don't know, what do you recommend ?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 29, 2018, 3:59pm UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/6 "2018-10-29T15:59:00Z")

</div>

By "type" of painless script, I meant what part of the system are you trying to use a script in? An example request with a script, even if you just have "PUT SCRIPT HERE" for the actual script.

> I have a timestamp like : 1488350778.

Do you have a field with this value, or you are passing it into the script?

> I want to get the month, year, semester, trimester from this timestamp.

Month and year are doable, but semester and trimester and completely subjective (not part of any calendar system). You can certainly have fixed dates for the beginning/end of semester/trimester and detect if your given date falls within the range.

---

<div class="post-metadata">

**Author:** ![CodeWalker](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@CodeWalker](https://discuss.elastic.co/u/CodeWalker)\
**Post date:** [October 30, 2018, 8:26am UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/7 "2018-10-30T08:26:36Z")

</div>

I basically want to create new fields : month and year from the timestamp.  
Yes, I have a field containing the timestamp.  
The goal is to create selection fields in my kibana dashboard to filter by month and year.  
I also want to use month and year as dimension in my kibana visualisation.

---

<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:** [November 27, 2018, 8:26am UTC](https://discuss.elastic.co/t/convert-timestamp-to-date-painless-scripting/153025/8 "2018-11-27T08:26:49Z")

</div>

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