I would like to modify the following accumulative/sequential python code sample in field calculator in a Date Time field. I think this is the correct code to use but let me know if not. The Date Time field is formatted like so: 30/01/2015 18:30:00. I need to add increments of 162 seconds to the time portion of the value for 24 records. The date will not change.
Here is the code sample copied from http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000004s000000 and I've put my field name 'Date_Time) in
Expression:
accumulate(!Date_Time!)
Expression Type:
PYTHON_9.3
Code Block:
total = 0
def accumulate(increment):
global total
if total:
total += increment
else:
total = increment
return total
I know that I can change a single row's Date_Time value once by running this syntax in field calculator: DateAdd ("s", 162, [Date_Time] ) but I need to loop through the values in the Date_Time column starting with 30/01/2015 18:30:00 for the first row and adding 162 seconds to the new total each time.
-
You can use simple expression, no code Dateserial(2015,01,30)+18.5/24+162* [FID]/24/60/60FelixIP– FelixIP2015年02月02日 02:53:52 +00:00Commented Feb 2, 2015 at 2:53
1 Answer 1
The way I just did this assumes that you are using a file geodatabase features class, with an OBJECTID field that starts at 1.
I've called my date field DateField
so change that for yours.
Parser: Python
Code Block:
def inc162secs(oid):
startDataObj = datetime.datetime.strptime("30/01/2015 18:30:00", "%d/%m/%Y %H:%M:%S")
return startDataObj + datetime.timedelta(seconds=( (oid - 1) * 162))
DateField =
inc162secs( !OBJECTID! )
The result was:
enter image description here
-
Thank you PolyGeo!!! Amazingly fast and completely satisfactory reply - your answer was exactly what I was looking for! I just applied it and !BAM! perfect results.GIS Adjunct in Training– GIS Adjunct in Training2015年02月02日 03:24:26 +00:00Commented Feb 2, 2015 at 3:24
-
By coincidence I wrote a similar example into one of my eLearning videos this morning so I had the answer at my fingertips.2015年02月02日 03:27:17 +00:00Commented Feb 2, 2015 at 3:27
Explore related questions
See similar questions with these tags.