Adding a new field in ArcMap from data of an existing field

3279
3
Jump to solution
07-11-2012 11:15 AM
KimballHansen
New Contributor
I want to create a new field with years data. There is an existing field with the timestamp YYYYMMDDHHMM. But I just want the years from that field to the new field. That way I can display just the years when I create a graph. IS there anyone that can explain the code to do this. I am not a programmer and need the scripts or python code explained as if I were a 5-year old. Thank you.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucasDanzinger
Esri Frequent Contributor
Hello,

I think the Calculate Field Examples article might be helpful for you. Create your new field, right click the field name and select Field Calculator. Select Python at the top for your parser. Double click the date field you are pulling the information from and this should add that field name to the block. Then, add [-4:] to grab the last 4 characters from that field.

For example, if your original date field was called "Date", the field calculation would look like this: !Date![-4:]

This topic is covered in the above mentioned article in the 4th example under "Simple Calculations".

I'm sure there are other ways as well.

View solution in original post

0 Kudos
3 Replies
LucasDanzinger
Esri Frequent Contributor
Hello,

I think the Calculate Field Examples article might be helpful for you. Create your new field, right click the field name and select Field Calculator. Select Python at the top for your parser. Double click the date field you are pulling the information from and this should add that field name to the block. Then, add [-4:] to grab the last 4 characters from that field.

For example, if your original date field was called "Date", the field calculation would look like this: !Date![-4:]

This topic is covered in the above mentioned article in the 4th example under "Simple Calculations".

I'm sure there are other ways as well.
0 Kudos
KimballHansen
New Contributor
Thanks. It worked. Do you know how to parse out each year, month, day, hour, minute from this one field into their own separate field? Since this is in the YYYYMMDDHHMM timestamp,  I would like to put them in their own field.
0 Kudos
BruceNielsen
Occasional Contributor III
Expanding on Lucas' example using the field calculator (everything after the # is a comment, don't include it):
!Date![:4]   #Extracts the Year
!Date![4:6]  #Extracts the Month
!Date![6:8]  #Extracts the Day
!Date![8:10] #Extracts the Hour
!Date![10:]  #Extracts the Minute
0 Kudos