How to calculate date field in python script - formatting question

16189
10
02-11-2011 11:10 AM
jduval
by
New Contributor II
I am trying to calculate a DATE field on a feature class in a personal geodatabase.  I have a date variable that is a string: distDate = "2011-02-11"

I am not able to figure out the proper formatting to get it to work in a python script.  This is what I have, but it does not update with this date, nor does it error out.  In ArcMap, I can use calculate field on the attribute table and successfully update the date with #2011-02-11# (VB).

arcpy.CalculateField_management (inFC, "DistDate", "#"+distDate+"#")

I could not find help on the ESRI support site, nor by just googling it.  Does anybody have suggestions?

Julie
GISP
Tags (2)
0 Kudos
10 Replies
NiklasNorrthon
Occasional Contributor III
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

See python docs for the date and datetime modules for details. strptime and strftime to go between strings and datetime.
0 Kudos
deleted-user-IR249IovB3CN
New Contributor III
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?
0 Kudos
curtvprice
MVP Esteemed Contributor
  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?


An approach that may help both of you is to try the VBScript DateSerial function:

DateSerial(2011,02,11)
DateSerial([yearfield],[monthfield],[dayfield])


Sometimes VBScript is easier, sometimes Python is easier. I'm really happy Esri is letting us have the option!
0 Kudos
deleted-user-IR249IovB3CN
New Contributor III
Thanks for the VB Dateserial function. I did not find reference to that in all of my searching. Before I saw this response 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
0 Kudos
curtvprice
MVP Esteemed Contributor
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!
0 Kudos
LucaMoiana
New Contributor
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?
0 Kudos
SolomonPulapkura
Occasional Contributor III
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 should give you _ "todays date"_località
0 Kudos
deleted-user-IR249IovB3CN
New Contributor III
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
0 Kudos
DhanushkaJayamaha
New Contributor
Hi Guys,

Sorry to jump in middle. I have functionality in a script that handles date and check for correct date any tips on how to check 2 different dates in the same layer. This is the sample and I�??m very new to scripting

#Field, Year, Month, Day
checker.checkGDBFieldForRightDate("SURVEY_DATE", 2012, 07, 24)

Thank You
0 Kudos