# How to convert string to date using kibana painless script

**URL:** <https://discuss.elastic.co/t/how-to-convert-string-to-date-using-kibana-painless-script/164169>\
**Category:** Kibana\
**Created:** [January 14, 2019, 3:54pm UTC](https://discuss.elastic.co/t/how-to-convert-string-to-date-using-kibana-painless-script/164169 "2019-01-14T15:54:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mouli632](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mouli632](https://discuss.elastic.co/u/mouli632)\
**Post date:** [January 14, 2019, 3:54pm UTC](https://discuss.elastic.co/t/how-to-convert-string-to-date-using-kibana-painless-script/164169/1 "2019-01-14T15:54:35Z")

</div>

I have string fields in my index like  
"repliedTs": "2018-12-27T10:58:28.423-05:00",  
"receivedTs": "2018-12-27T10:58:27.168-05:00"  
i am trying to write kibana painless script to convert string type to date and find the difference, but could not get success. could please help me  
here is my code...  
(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S").parse(doc['repliedTs'].value).getTime()) - (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S").parse(doc['receivedTs'].value).getTime())

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [January 15, 2019, 2:14am UTC](https://discuss.elastic.co/t/how-to-convert-string-to-date-using-kibana-painless-script/164169/2 "2019-01-15T02:14:22Z")

</div>

These fields would be dynamically mapped to date type by ES. Try:

```auto
doc['repliedTs'].value.millis - doc['receivedTs'].value.millis

```

This will retrieve the difference in milliseconds.

---

<div class="post-metadata">

**Author:** ![mouli632](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mouli632](https://discuss.elastic.co/u/mouli632)\
**Post date:** [January 17, 2019, 6:39pm UTC](https://discuss.elastic.co/t/how-to-convert-string-to-date-using-kibana-painless-script/164169/3 "2019-01-17T18:39:15Z")

</div>

Thanks for reply jen-Huang,  
I tried as you suggested but not working  
doc['repliedTs'].value.millis - doc['receivedTs'].value.millis  
Is there any other way to convert string to date?

---

<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 14, 2019, 6:54pm UTC](https://discuss.elastic.co/t/how-to-convert-string-to-date-using-kibana-painless-script/164169/4 "2019-02-14T18:54:33Z")

</div>

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