How to Pad a String (date)?

Hi,
I need to have a date in a format such as MM.dd.yyyy and my data row has some different scenarios like:
M.d.yyyy
M.dd.yyyy
MM.d.yyyy
I’ve tried with colum extentions and the following script:

var col = column(“Conf.Del.Date”)
if (length(col) == 9) {
“0” + col[0] + col[1]+col[2]+col[3]+col[4]+col[5]+col[6]+col[7]+col[8]+col[9]
}
else if (length(col) == 8) {
“0”+ col[0] + col[1]+“0”+col[2]+col[3]+col[4]+col[5]+col[6]+col[7]+col[8]
}

But it seems that when I have MM.d.yyyy i will end with "0"MM.d.yyyy

How can I solve this?

Thanks

Hi and welcome!
I’m not great with regular expressions, so one solution I could see is to split the column up into multiple columns first. This may make writing a regular expression easier to form and troubleshoot. You can use the Cell Splitter node to split the date column into 3 separate columns using “.” or “/” as a delimiter (you may need to convert the Date column to String first). Then use Column Expressions node to pad the 3 new columns if necessary. Lastly, use the Column Aggregate node with “.” or “/” as the Value delimiter to recombine into a date format.

1 Like

Hi there @ANDREACAGNAZZI,

welcome to KNIME Community Forum!

I would use following approach. Convert strings to legacy Date&Time format (MM.dd.yyyy as a format) and then back to string (if you need it in string format) or in actual Date&Time format.

If any questions feel free to ask.

Br,
Ivan

2 Likes

Thank you very much.

Thank you very much.

2 Likes

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