# How can i zero pad a string in painless

**URL:** <https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914>\
**Category:** Kibana\
**Created:** [September 27, 2017, 12:43am UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914 "2017-09-27T00:43:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danaronson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danaronson/32/7043_2.png) [@danaronson](https://discuss.elastic.co/u/danaronson)\
**Post date:** [September 27, 2017, 12:43am UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914/1 "2017-09-27T00:43:16Z")

</div>

I am adding a scripted field to create a URL and I need to zero pad some date values (since data.date.dayOfMonth and related functions don't). Any pointers to how I can do this?

--dan

---

<div class="post-metadata">

**Author:** ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)\
**Post date:** [September 27, 2017, 2:30pm UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914/2 "2017-09-27T14:30:29Z")

</div>

Hey there,

You should be able to zero pad data values using the format pattern configuration within your scripted field:

 ![13 AM](https://us1.discourse-cdn.com/elastic/original/3X/4/d/4d4df1d429e6fe1bc822365850d1de0d598ed551.png)

Please see the [Moment JS docs](http://momentjs.com/docs/#/displaying/format/) on specific formatting options.

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![danaronson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danaronson/32/7043_2.png) [@danaronson](https://discuss.elastic.co/u/danaronson)\
**Post date:** [September 27, 2017, 6:53pm UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914/3 "2017-09-27T18:53:27Z")

</div>

That doesn't help me because the padded value that is going to be part of a string.

--dan

---

<div class="post-metadata">

**Author:** ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)\
**Post date:** [September 27, 2017, 7:56pm UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914/4 "2017-09-27T19:56:09Z")

</div>

Hi @danaronson,

My mistake, I misunderstood the use case.

What about setting up the scripted url field with this script:

```auto
def newFormat = new SimpleDateFormat('yyyy-MM-dd');
def oldFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
def date = oldFormat.parse(doc['@timestamp'].value.toString());
return newFormat.format(date);

```

---

<div class="post-metadata">

**Author:** ![danaronson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danaronson/32/7043_2.png) [@danaronson](https://discuss.elastic.co/u/danaronson)\
**Post date:** [October 7, 2017, 10:51pm UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914/5 "2017-10-07T22:51:01Z")

</div>

That looks like it would work. I'll try it out.

---

<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 4, 2017, 10:51pm UTC](https://discuss.elastic.co/t/how-can-i-zero-pad-a-string-in-painless/101914/6 "2017-11-04T22:51:18Z")

</div>

Closed on November 4, 2017, 10:51pm UTC

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