1

I have a String field 'INTERVAL' giving the two dates used for a difference image in the form "YYYYMMDD_YYYYMMDD". I would like to populate a second field 'MONTH' giving the month of the median date of the two dates in 'INTERVAL' i.e. date1 + (date2 - date1 / 2) for example for 'INTERVAL' = 20180325_20180420 , the median month would be 4.

Ideally prefer to do it with Calculate Value in Modelbuilder.

I am not Python savvy so dealing with dates is particularly vexing.

ArcMap 10.4.1

2
  • You mention the Calculate Value tool in your question body but you have a tag for the Field Calculator (or Calculate Field tool) which are you using to try and do this, and what does your code so far look like?
    – PolyGeo
    Commented Apr 24, 2018 at 6:37
  • Thanks Polygeo. I promise I will first try to do it myself next time. Commented Apr 27, 2018 at 0:32

1 Answer 1

3

Use Field Calculator with Python parser and the datetime module:

The datetime module supplies classes for manipulating dates and times in both simple and complex ways

Pre-Logic Script Code:

def mdate(dates):
    import datetime
    #Split into two strings
    first, second = dates.split('_')
    #Convert strings into datetime
    first = datetime.datetime.strptime(first,'%Y%m%d')
    second = datetime.datetime.strptime(second,'%Y%m%d')
    #Calculate mean month
    average_delta = (second - first) / 2
    return (first + average_delta).month

Call it, change !Dates! to match the name of your field:

mdate(!Dates!)

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.