Date - Today's date in Python?

13351
20
Jump to solution
06-13-2013 09:48 AM
ionarawilson1
Occasional Contributor III
Hi guys,

I am writing a python script that adds a feature to geodatabase feature class. Some fields the user has to enter manually and some are automatic. However the date field output is an incorrect date of 7/5/1905. Does anybody have any idea why this is happening? I have tried everything I could find and nothing worked. I am using a file geodatabase, the date field is of date type and my computer's setting has the correct today's date. Thank you for any help!

Here is the code

#Calculate Date Field expression = time.strftime("%Y") arcpy.CalculateField_management("Boundary", "Date", expression, "PYTHON")


If I change the code I get a date of 3/29/1969. Here is the code for that:

#Calculate Date Field                                                     import time                                                               import datetime                                                           now = datetime.date.today()                                               expression = now.strftime("%m%d%y")                                                                                                                 arcpy.CalculateField_management("Boundary", "Date", expression, "PYTHON")
Tags (2)
20 Replies
ionarawilson1
Occasional Contributor III
Thank you Rhett. Is there any chance you know why Jame's code is not working? Thanks
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thank you James, but I tried your code and I get a null value for the date field. I am not sure if the calculate field management method is recognizing as a string, a real expression.

Here is my code

#Calculate Date Field                                                   
import time                                                             
import datetime                                                         
#current date                                                           
d1 = datetime.today()                                                   
_currdate = datetime.strftime(d1, "%Y-%m-%d")                           
                                                               
                                                                        
arcpy.CalculateField_management("Boundary", "Date", _currdate, "PYTHON")


If it is a date type field, then just use d1 variable in your calculate process.

arcpy.CalculateField_management("Boundary", "Date", d1, "PYTHON")
0 Kudos
ionarawilson1
Occasional Contributor III
It is a date type but that did not work, I got a null value
0 Kudos
RhettZufelt
MVP Frequent Contributor
ionara,

Don't have time to dive into this at the moment, but it could be because the datetime module has no attribute "today" so
d1 = datetime.today()
will error out and not assign the variable no matter what imports I try in the IDE.



Not where I can look it up at the moment, but you should look into the datetime attributes and options.  believe something like


d1 =  = date.today()


But don't remember exactly offhand.

R_
0 Kudos
ionarawilson1
Occasional Contributor III
This worked for me! Thanks guys!!!!

arcpy.CalculateField_management("Boundary", "Date", "time.strftime('%d/%m/%Y')", "PYTHON_9.3", "")
0 Kudos
JamesCrandall
MVP Frequent Contributor
It is a date type but that did not work, I got a null value


Add another "datetime" to the line:

#current date
d1 = datetime.datetime.today()
print str(d1)


I get: 2013-06-13 16:04:42.57100
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you James. I just need the date! Thanks again! My problem was with the single and double quotes.
0 Kudos
RhettZufelt
MVP Frequent Contributor
Yep, that did it.

Guess will all the options available, just pick one that works.:cool:

R_
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Rhett and James!!! 😄
0 Kudos
NiklasNorrthon
Occasional Contributor III
Calling CalculateField from python is asking for trouble. In my experience it is always, without exceptions, much easier to use cursors instead. Especially with the arcpy.da cursors introduced in ArcGIS 10.1
0 Kudos