1

I'm attempting to retrieve the year from the following date format: 1/1/1988 I've tried the following but it does not work str(1/1/1988)[4:] and I've also tried str(1//1//1988)[4:]

  • 1
    In what context? A Python script? Can you post it? In field calculator? – Jason Scheirer Oct 1 '13 at 16:26
  • 1
    Can we assume that this is coming from a field of "date" type? – RyanDalton Oct 1 '13 at 18:46
  • I'm using a Python update cursor to reference a date field which has dates in the following format 5/1/1988 and update another field with only the year of the date 5/1/1988 which in this case is 1988. @JasonScheirer – lemuel Oct 1 '13 at 19:25
  • @RyanDalton you're right. – lemuel Oct 1 '13 at 19:26
  • 2
    What ArcGIS for Desktop version are you using? Are you using the Data Access module? Can you post a code snippet to show how you are using a "Python update cursor to reference a date field"? Please edit these details into your question so that a potential answerer only needs to read that to get the full picture. – PolyGeo Oct 1 '13 at 20:46
8

This sort of question is better answered in StackOverflow but the answer is straight-forward enough so I'll give you a hint here.

Your date is not a date as far as Python is concerned but a division sum - which is the main reason why it doesn't work. Your code also won't give you the last four digits. You need '[-4:]' (yours gives everything except the first four characters of the string). You need to cast your date as a date using the datetime module. Then to convert a properly cast date as a string in the format you have in your original post you call myDate.strftime("%d/%m/%Y")[-4:]

  • I've not dealt with dates all that much in Python, how would I cast my date as a date using the datetime module – lemuel Oct 1 '13 at 18:27
  • 1
    If the date is coming back as a Python datetime object (which if the field is of type date it will be - see @blah238 's answer to this question) then it will have a year attribute – om_henners Oct 2 '13 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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