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:]
-
1In what context? A Python script? Can you post it? In field calculator?– Jason ScheirerOct 1, 2013 at 16:26
-
1Can we assume that this is coming from a field of "date" type?– RyanKDaltonOct 1, 2013 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– lemuelOct 1, 2013 at 19:25
-
@RyanDalton you're right.– lemuelOct 1, 2013 at 19:26
-
2What 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, 2013 at 20:46
1 Answer
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– lemuelOct 1, 2013 at 18:27
-
1If 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 ayear
attribute Oct 2, 2013 at 5:47