How can i zero pad a string in painless

(Danaronson) #1

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


(Chris Roberson) #2

Hey there,

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

Please see the Moment JS docs on specific formatting options.


(Danaronson) #3

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


(Chris Roberson) #4

Hi @danaronson,

My mistake, I misunderstood the use case.

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

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);

(Danaronson) #5

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

(system) #6

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