3

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 2, 2015 at 2:07
1
  • You can use simple expression, no code Dateserial(2015,01,30)+18.5/24+162* [FID]/24/60/60 Commented Feb 2, 2015 at 2:53

1 Answer 1

3

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

answered Feb 2, 2015 at 3:00
2
  • 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. Commented 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. Commented Feb 2, 2015 at 3:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.