This expression works in the field calculator to give me the hour from the datetime field called dtrepor
arcpy.time.ParseDateTimeString(!dtrepor!).strftime('%H')
But how do I call it in python?
I have tried the following:
hour = arcpy.time.ParseDateTimeString(!dtrepor!).strftime('%H')
arcpy.CalculateField_management(saved_Incidents, "mil_time", hour , "PYTHON")
But it gives me a syntax error?
Best Answer
The easiest way to find out how to reproduce something in python is to run it once in ArcMap using the ArcToolbox tool (or in this case, the Field Calculator), and then choosing Copy Python Snippet from the geoprocessing results.
Run the Field Calculator
Open the Results pane (Geoprocessing > Results)
Find the corresponding tool results, right-click and select Copy As Python Snippet
Now paste that Python snippet into your IDE. This will be the code you want to use in python.
You can also rewrite how this is written in python now you know how it should be structured (I have used your values in this one):