TransWikia.com

Calculating dates and using arcpy.CalculateField_management(

Geographic Information Systems Asked on April 21, 2021

I am in the process of converting my scripts from python 2.7 to 3.x

I am trying to calculate a date field with a datetime object

exactScriptTime = datetime.datetime.now()

exactScriptTime = exactScriptTime.strftime("%d %b %Y %X")
arcpy.CalculateField_management(os.path.join(tempWorkspace, "DWWEB_Joined"), "GIS_TIMESTAMP", "#" + exactScriptTime + "#" , "VB", "") #WORKS WITH VB
arcpy.CalculateField_management(os.path.join(tempWorkspace, "DWWEB_Joined"), "GIS_TIMESTAMP", "#" + exactScriptTime + "#" , "PYTHON", "") #DOESNT WORK

The error that comes back is

arcpy.CalculateField_management(os.path.join(tempWorkspace, "DWWEB_Joined"), "GIS_TIMESTAMP", "#" + exactScriptTime + "#" , "PYTHON", "")
Traceback (most recent call last):
  File "d:ScheduledTasksScriptstest.py, line 348, in <module>
    arcpy.CalculateField_management(os.path.join(tempWorkspace, "DWWEB_Joined"), "GIS_TIMESTAMP", "#" + exactScriptTime + "#" , "PYTHON", "")
  File "C:Program FilesArcGISProResourcesArcPyarcpymanagement.py", line 3907, in CalculateField
    raise e
  File "C:Program FilesArcGISProResourcesArcPyarcpymanagement.py", line 3904, in CalculateField
    retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block), True)))
  File "C:Program FilesArcGISProResourcesArcPyarcpygeoprocessing_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000539:   File "<expression>", line 1
    #10 Nov 2020 15:47:18#
                         ^
SyntaxError: unexpected EOF while parsing

Failed to execute (CalculateField).


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:Program FilesArcGISProResourcesArcPyarcpymanagement.py", line 3907, in CalculateField
    raise e
  File "C:Program FilesArcGISProResourcesArcPyarcpymanagement.py", line 3904, in CalculateField
    retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block), True)))
  File "C:Program FilesArcGISProResourcesArcPyarcpygeoprocessing_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000539: Traceback (most recent call last):
  File "d:ScheduledTasksScriptstest.py", line 348, in <module>
    arcpy.CalculateField_management(os.path.join(tempWorkspace, "DWWEB_Joined"), "GIS_TIMESTAMP", "#" + exactScriptTime + "#" , "PYTHON", "")
  File "C:Program FilesArcGISProResourcesArcPyarcpymanagement.py", line 3907, in CalculateField
    raise e
  File "C:Program FilesArcGISProResourcesArcPyarcpymanagement.py", line 3904, in CalculateField
    retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block), True)))
  File "C:Program FilesArcGISProResourcesArcPyarcpygeoprocessing_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000539:   File "<expression>", line 1
    #10 Nov 2020 15:47:18#
                         ^
SyntaxError: unexpected EOF while parsing

Failed to execute (CalculateField).


During handling of the above exception, another exception occurred:

  File "<expression>", line 1
    #10 Nov 2020 15:47:18#
                         ^
SyntaxError: unexpected EOF while parsing

Failed to execute (CalculateField).

I’ve also tried

arcpy.CalculateField_management(os.path.join(tempWorkspace, "DWWEB_Joined"), "GIS_TIMESTAMP", exactScriptTime , "PYTHON", "")

I have also converted the exactScriptTime variable to a datetime object datetime.datetime.strptime(exactScriptTime,"%d %b %Y %H:%M:%S"), but still makes no difference.

In essence i want to keep the format to be "%d/%m/%Y %H:%M:%S" for the field i want to calculate

How do I get around this?

One Answer

"Any Python datetime function can be used to calculate a date"

Try:

#import arcpy, datetime

fc = r'C:data.gdbfeatures123'
date_field = 'datefield'

now = 'datetime.datetime.now()'

arcpy.CalculateField_management(in_table=fc, field=date_field,
                                expression=now)

enter image description here

Correct answer by BERA on April 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP