3

I would like some information on how to add a date to an attribute table, by adding a user specified date in a script tool that will update the polygons attribute table. In short I have two polygons that I wish to calculate the difference in time, in hours between them. The date is determined by specification inside a script box, seen in the image below.

Screenshot of tool

I would like to use code that goes along with my tool and I need to figure out what I'm missing in the following code; (This is the simple code for the tool)

import arcpy 

Mother1 = sys.argv[1]
date1= sys.argv[2] ### I believe date style is in format mm/dd/yyyy hh:mm:ss 
Mother2 = sys.argv[3]
date2 = sys.argv[4]

try:
    arcpy.AddField_management(Mother1,"Date","DATE","#","#","#","#","NULLABLE","NON_REQUIRED","#")
    arcpy.AddField_management(Mother2,"Date","DATE","#","#","#","#","NULLABLE","NON_REQUIRED","#")

Then, I tried adding the date to the field such as this;

arcpy.CalculateField_management(Mother1, "Date", date1)
arcpy.CalculateField_management(Mother2, "Date", date2)

But that did not work.

I would like to add the date to the attribute table and then also be able to calculate the difference between the two dates, but need to get the date into the attribute table before I can calculate anything?

1 Answer 1

3

I would not assume it is a date. I would make sure the date and time is captured as a string and create a python expression using the strptime function and use a PYTHON_9.3 calculation to have the entire date and time calculated. VB Date expressions should be in the format of #date#, but they are not reliable, since they only preserve the date, not the time, and you cannot control how they interpret different string date formats, like you can with the strptime function using a PYTHON_9.3 field calculation. (Edit: I adjusted the try block location to make sure that any date conversion error is handled in the except block and I added logic to determine if the Date field should be added or if it already exists). The revised code now formats the strptime function expression as a string that allows the Field Calculator to do the date conversion using the strptime function, not the script:

import arcpy

from datetime import datetime

try:

    Mother1 = arcpy.GetParameterAsText(0)
    print arcpy.GetParameterAsText(1)
    ### Confirm from print that date string is in the format yyyy-mm-dd hh:mm:ss
    ### Make a Field Calculation expression that will use strptime to get a datetime. 
    date1 = "datetime.datetime.strptime('" + arcpy.GetParameterAsText(1) + "', '%Y-%m-%d %H:%M:%S')"
    ### VB Script calculations use "#date#" strings to create dates, but not datetime 
    ### date1 = '#' + arcpy.GetParameterAsText(1) + '#'
    Mother2 = arcpy.GetParameterAsText(2)
    date2 = "datetime.datetime.strptime('" + arcpy.GetParameterAsText(3) + "', '%Y-%m-%d %H:%M:%S')"
    ### VB Script calculations use "#date#" strings to create dates, but not datetime
    ### date2 = '#' + arcpy.GetParameterAsText(3) + '#'

    fields = arcpy.ListFields(Mother1)
    addfield = True
    for field in fields:
        if field.name.upper() == "DATE":
            addfield = False
    if addfield:
        arcpy.AddField_management(Mother1,"Date","DATE","#","#","#","#","NULLABLE","NON_REQUIRED","#")

    fields = arcpy.ListFields(Mother2)
    addfield = True
    for field in fields:
        if field.name.upper() == "DATE":
            addfield = False
    if addfield:
        arcpy.AddField_management(Mother2,"Date","DATE","#","#","#","#","NULLABLE","NON_REQUIRED","#")

    arcpy.CalculateField_management(Mother1, "Date", date1, "PYTHON_9.3")
    arcpy.CalculateField_management(Mother2, "Date", date2, "PYTHON_9.3")
except Exception as e:
    print e.message

    # If using this code within a script tool, AddError can be used to return messages 
    #   back to a script tool.  If not, AddError will have no effect.
    arcpy.AddError(e.message)
11
  • 1
    +1 I was just starting to look at this and my first comment on the question was going to be that I would use arcpy.GetParameterAsText() rather than sys.argv[] which you have covered. However, the focus of this question should always have been date/string formatting and you have used a technique that I am sure I will be looking back at next time I need to handle dates in ArcPy.
    – PolyGeo
    Commented Dec 7, 2014 at 3:28
  • 2
    If you printing date1 after the conversion then that makes no difference (it is a valid date if the conversion succeeded). It only makes a difference what print arcpy.GetParameterAsText(1) prints like. Are you saying that the print arcpy.GetParameterAsText(1) strings is 2014-12-01 22:37:28? If so that isn't a problem. I just have to match that format, so I changed the strptime format to: date1 = datetime.strptime(arcpy.GetParameterAsText(1), '%Y-%m-%d %H:%M:%S') Commented Dec 7, 2014 at 9:25
  • 1
    You cannot use a date string in the calculation without enclosing it in the format "#date#" The hashtags enclosing the string tell the field calculator to use it as a date. So for the field calculation you may not want a real date and just use: date1 = '#' + arcpy.GetParameterAsText(1) + '#'. You also could use an updatecursor to accomplish same thing as a field calculation and the cursor would correctly accept the real python date. The real python dates could also do the date difference operation without every storing the actual dates in a table. Commented Dec 7, 2014 at 15:55
  • 1
    The hashtage #date# format applies to VB Script calculations, which is what you are using by not specifying the calculation type (the parameter after the date expression). For Python calculations I believe you would have to set the expression up to use the strptime function to get the field calculator to treat the string as a date, but I believe that function expression needs to be a string expression to work in a script so that the field calculator does the date conversion, not the script. It trips me up often, since it works one way in Desktop and another in a script. Commented Dec 7, 2014 at 16:08
  • 1
    The calculation should now work if you use my latest revised code. I still recommend the strptime function expression over the VB Script #date# expression, since the strptime can be adjusted to handle many different date formats, while the VB Script #date# function does not let you specify the date format it accepts. Commented Dec 7, 2014 at 16:45

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.