[GIS] Concatenate Multiple String Fields and Convert to Date field using ArcGIS Field Calculator

arcgis-desktopdatefield-calculatormodelbuilderpython

I'm Working in Model builder using the calculate value tool. I have three columns – SignatureDay, SignatureMonth, SignatureYear – All three are string data types and all consist of just numbers for characters. I can combine most values easily into a single field and then convert that field into a date. My problem is that some values will not convert.

For instance:

SignatureDay   SignatureMonth    SignatureYear       Convert                SubmitDate
01                 05                 2014           20140105                1/5/2014
05                 05                 2014           20140505                5/5/2014
1                   5                 2014           201415

The code used to give me the values in the field

"Convert" = "{}{}{}".format(!SignatureYear!,!SignatureMonth!, !SignatureDay!)

for some reason the values with single digits and no zeroes in front, will not participate in the final output to a date type in the field "SubmitDate".

I've tried going through with the .replace() function but this table is dynamic and does not always have the same values.
So –

!SignatureDay!.replace('1', '01').replace('2', '02')

….etc does not work, I would assume because all values 1-31(for every day possible) may not exist. Also has to be a better way than repeating that 31 times. I'm thinking there is an loop or something that could be run?

I'm a little lost on this one, my python experience is very limited as is my programming experience in general.

Best Answer

You do not need to do replace. You just need to modify the format string.

"Convert" = "{}{:02d}{:02d}".format(!SignatureYear!,!SignatureMonth!, !SignatureDay!)