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.

  • What is the field type of the existing field? String? Datetime?
    – AHigh
    Commented Jul 11, 2012 at 19:10

2 Answers 2


Assuming your existing timestamp field is a string:

  1. Add a new field (Open the attribute table of your feature class>Click Upper Left Dropdown>Add Field) as a long integer.

  2. Right click new field and select "Calculate Field".

  3. Change your parser to Python and use the following expression:


Note that you need to replace the "ExistingTimeStampField" between the exclamations with the actual name of your field (should be able to auto-populate in the Fields list box). This will trim off the leading 4 characters from your existing field which I presume will always be the 4-digit year.

Edit: If you wish to break out more information you can simply update the indices of your query like such (Note that my original [:4] is the same as saying [0:4]):


This would return the MM in YYYYMMDDHHMMSS. [6:8] would return the DD, and so on. See here for a more detailed explanation of substrings: http://docs.python.org/tutorial/introduction.html#strings

  • Perfect, Thank you. Do you know how to parse the rest of the time (Months, days, hours, minutes) into separate fields?
    – Kimball
    Commented Jul 11, 2012 at 20:12
  • 1
    Do everything up until step 3 and then change the syntax to this: !ExistingTimeStampField![4:6] for months, !ExistingTimeStampField![6:8] for days, and so on.
    – AHigh
    Commented Jul 11, 2012 at 20:17

If your field type is a string that contains data formatted exactly as you said, then filling your new field is a relatively simple matter of parsing out the string.

Since you did not specify, I'll give options in both the ArcGIS, and Quantum GIS software packages, depending on what you are using.


In this case, the Left function would be appropriate, in which you specify the source text, and then the number of characters that you want to use from the left of the source.

Right click on the name of your new field, and choose "Field Calculator". Type in what you see from the screenshot below. Replace [CONST_DATE] with your column name. This is a VB Script option. It looks like a Python style answer has also been posted.

enter image description here

Quantum GIS

  1. Toggle Editing Mode to On - Ctrl E
  2. Open Attribute Table
  3. Open Field Calculator Field Calculator button
  4. In this case, the appropriate function is substr. In this function, you choose the source text, then specify the starting point, and the length that you want to capture. This uses an index where the first position in the string is 1.
  5. Enter text into Field Calculator box as in the screenshot below. Again, replace the field name CONST_DATE, with your appropriate source column. Also, on the right, choose the appropriate column that you are filling the date into. Field Calculator Dialog
  6. Save Edits - Ctrl S
  7. Toggle Editing Mode Off - Ctrl E
  • Perfect, Thank you. Do you know how to parse the rest of the time (Months, days, hours, minutes) into separate fields?
    – Kimball
    Commented Jul 11, 2012 at 20:12
  • And yes I'm using ArcMap, sorry for the confusion. I was intimidated by that long answer from QGIS.
    – Kimball
    Commented Jul 11, 2012 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.