How can i zero pad a string in painless

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

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.

Thanks,
Chris

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

--dan

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

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

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