import datetime import arcpy arcpy.env.workspace = 'c:/temp/my_gdb.gdb' fc = 'my_fc' fld = 'my_date_field' cur = arcpy.InsertCursor(fc) row = cur.newRow() row.setValue(fld, datetime.datetime.now()) cur.insertRow(row) row = cur.newRow() row.setValue(fld, datetime.datetime(2010,12,25,12,34) cur.insertRow(row) row = cur.newRow() row.setValue(fld, datetime.datetime.now()) cur.insertRow(row) a_date = '2010-12-25 13:57:01' date_fmt = '%Y-%m-%d %H:%M:%S' row = cur.newRow() row.setValue(fld, datetime.datetime.strptime(a_date, date_fmt) cur.insertRow(row) del row del cur
In ArcMap, I can use calculate field on the attribute table and successfully update the date with #2011-02-11# (VB).
Does anyone know how to use datetime.strptime to calculate a date field using 3 fields that represent year, month, and day? Or perhaps using datetime.strptime to calculate a datefield using a string field of date yyyy/mm/dd?
DateSerial(2011,02,11) DateSerial([yearfield],[monthfield],[dayfield])
Matt, thanks for the update. Before the edit window closes, could you enclose your python code in [thread=48475]code tags[/thread] to make it more useful for others?
Just a note, I like to store datetime in GIS datasets and database tables as standard-format strings "yyyy-mm-dd hh:mm:ss" and calculate date values when needed in an analysis or selection, because it seems every operating system and file format seems to have its own scheme or limitations!
Not an issue if all your data is in one database, but if you need to move the table from one place another, it seems data stored in date format can be more trouble than it is worth!
Sorry but I have a similar problem, I'm writing a script to import multiple .shp to a gdb and I'd like to add date in from of the name (ex: località ->_ 120705_località)
Any idea?
import time # use it in a string as needed e.g. arcpy.AddMessage("_{0}_località".format(time.strftime("%m%d%y")))
This is the solution a colleague of mine came up with to calculate the new date field from 3 exisitng fields of month, day, and year.
import system modules import arcpy from datetime import datetime, date, time # set workspace environment env = arcpy.env.workspace = "C:/Files/GIS/Projects/Hypoxia/Shrimp_Hypoxia/AnalysisData.gdb" #Set OverWrite if files already exist arcpy.env.overwriteOutput = True # set local variables inFile = "Full_Dataset_Albers" #Add date field to inFile arcpy.AddField_management(inFile, "DateField","DATE") #Create an UpdateCursor rows = arcpy.UpdateCursor(inFile) #Iterate through each record and update the DateField date with the combination of the 3 fields that provide the components of the date. for row in rows: d_obj = datetime(row.getValue("yr"), row.getValue("mon"), row.getValue("day")) row.DateField = d_obj rows.updateRow(row) del row, rows