Add seconds to a datetime in arcpy field calculator

3915
2
Jump to solution
12-16-2015 10:02 PM
IanIrmischer
New Contributor III

Hi, I have a date field called date with date times in format 4/1/2016 6:40:00 AM

I have a second field called times that is a double with numbers of seconds.

I would like to use arcpy.CalculateField_management to add the seconds to the time in a new date field called futuretime.

I can do this in the field calculator in VB using:

DateAdd ( "s", [times], [date] )

But I am trying to use a script to run through a bunch of files in a for loop.

I started with

for files in allFC:

     arcpy.CalculateField_management(files, "futuretime",?????, "PYTHON_9.3",????)

but I can not figure out the code to fill into the ?????s

Thanks for any help,

Ian

0 Kudos
1 Solution

Accepted Solutions
JonMorris2
Occasional Contributor II

You can use timedelta to add the two attributes together. For example,

arcpy.CalculateField_management(files, "futuretime", "datetime.datetime.strptime(!date!,'%d/%m/%Y %H:%M:%S') + datetime.timedelta(seconds=!seconds!)", "PYTHON_9.3")

You may want to check the datetime module to make sure I've got the format right.

View solution in original post

2 Replies
JonMorris2
Occasional Contributor II

You can use timedelta to add the two attributes together. For example,

arcpy.CalculateField_management(files, "futuretime", "datetime.datetime.strptime(!date!,'%d/%m/%Y %H:%M:%S') + datetime.timedelta(seconds=!seconds!)", "PYTHON_9.3")

You may want to check the datetime module to make sure I've got the format right.

IanIrmischer
New Contributor III

Jon, Thanks so much.  It worked perfectly.  Really appreciate the help.

Ian

0 Kudos