Hi Brad,
Thank you so much for your patience and assistance. However, I am still getting an error when I run the Calculate Field. However, now my error is:
ERROR 000539: Error running expression: Calc("6/16/2006") <type 'exceptions.ValueError'>: month must be in 1..12
Failed to execute (Calculate Field (3)).
I think this must mean my Date_Time field isn't in the correct format. Reading the ArcGIS help online it mentions that shapefiles and coverages store Dates differently as it only stores dates and not time. In anycase my dataset is a shapefile and I'm working in 9.3.1 if that makes a difference.
I apologize for not figuring this out myself, but I've having some trouble understanding the details of the code as I'm new to Python.
Thanks again for you help!
Let's start with this line:lstDate = map(int, strDate.split("/")) # create list of date integers D(D), M(M), YYYY
(note that comments are preceded by #, so anything following # is ignored)This is actually two commands joined as one: the "map" function and the "split" string method.Say you have a date of "6/16/2006". The string methodstrDate.split("/")
creates a Python list by splitting the string at the forward slash, with a result of ["6", "16", "2006"]. So now Python interprets the line as:lstDate = map(int, ["6", "16", "2006"])
The map function simply says "take each element in the list (second parameter) and pass it into the function defined in the first parameter". In this case, each element is passed into the int() function, converting the strings in the list to integers. So now Python interprets the line as:lstDate = [6, 16, 2006]
To access each element in the list you append an index number in square brackets to the list variable name. The index numbering starts with 0, so you can extract each value like this:
lstDate[0] = 6 # month
lstDate[1] = 16 # date
lstDate[2] = 2006 # year
Now jump into this line:objDate = datetime.date(lstDate[2], lstDate[1], lstDate[0]) # create date object
The date object is created by passing three parameters, as integers, to the datetime.date method in the following order:datetime.date(<year>, <month>, <date>)The date format in my data was DD/MM/YYYY, so lstDate[0] was day, lstDate[1], was month, and lstDate[2] was year. Therefore, the parameters are:datetime.date(lstDate[2], lstDate[1], lstDate[0])
Your data (and I didn't notice before my last post) is MM/DD/YYYY. Therefore, you simply need to change the order of the parameters to fix the error. objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1]) # create date object