Date - Today's date in Python?

9866
20
Jump to solution
06-13-2013 09:48 AM
ionarawilson1
Regular Contributor II
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)
1 Solution

Accepted Solutions
ionarawilson1
Regular Contributor II
This worked for me! Thanks guys!!!!

arcpy.CalculateField_management("Boundary", "Date", "time.strftime('%d/%m/%Y')", "PYTHON_9.3", "")

View solution in original post

0 Kudos
20 Replies
JamesCrandall
MVP Frequent Contributor
I am not entirely clear on what the issue is and what your desired result should be, but here is how you get a current date

#current date
d1 = datetime.today()
_currdate = datetime.strftime(d1, "%Y-%m-%d")

#date of 15 days ago from the current date
d2 = d1 - timedelta(15)
_pastdate = datetime.strftime(d2, "%Y-%m-%d")
AmandaBishop
Occasional Contributor

Does this python code also work in the field calculator inside of the attribute table?  I have an active edit session on my feature class in the geodatabase and I am trying to get the current date for editing.

0 Kudos
Zeke
by
Regular Contributor III

Is the field you're calculating a date type field or text? Either way, you can use these types of code snippets in Field Calculator, but you may want/need the Advanced codeblock to set up a def returning your value.

AmandaBishop
Occasional Contributor

Hello Greg,

After I posted this if found out that there is a default "data" field calculation in the field calculator tool.  It was handy and much easier than trying to write my own.

Thank you.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

As Greg said, it can be done... a snippet designed for a text field

>>> print datetime.strftime(datetime.today(),"%Y-%m-%d")

2014-12-10

>>>

as a function it would have to be

Parser:  Python

from datetime import datetime

def (dateFld):

     datetime.strftime(datetime.today(),"%Y-%m-%d")

Expression box:  !YourDateFieldName!

AmandaBishop
Occasional Contributor

Hey Dan,

After I posted this if found out that there is a default "data" field calculation in the field calculator tool.  It was handy and much easier than trying to write my own.

Thank you.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

0 Kudos
RhettZufelt
MVP Regular Contributor
A few options, depending on the format you want.



>>> from time import strftime
>>> strftime("%m-%d-%Y %H:%M:%S")
'06-13-2013 11:52:00'
>>> strftime("%m-%d-%Y")
'06-13-2013'
>>> strftime("%m%d%y")
'061313'
>>> strftime("%m%d%Y")
'06132013'
>>> strftime("%Y")
'2013'
>>>



R_
0 Kudos
ionarawilson1
Regular Contributor II
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")
0 Kudos