CÓMO

Rellenar un campo de fecha con una fecha actual utilizando Python

Last Published: July 20, 2023

Resumen

In ArcGIS Desktop, a date field can be populated with the current date using the Field Calculator in ArcMap or the Calculate function in ArcGIS Pro. The Python parser in the field calculator provides the time.strftime('%d/%m/%Y') function to do so. Alternatively, the current date can also be populated using the UpdateCursor() function.

Procedimiento

The following steps describe how to populate a field with the current date using a Python script.

  1. Import the necessary modules.
import arcpy, datetime
  1. Specify the location of the geodatabase and the desired field.
fc = r'C:\Test\Test1.gdb\Feature_1'
field = "Date_Field"
  1. Start a loop with the UpdateCursor() function to populate the selected field with the current date.
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])

The following shows the full script:

import arcpy, datetime

fc = r'C:\UsersUSER1\Desktop\Data\Database1.gdb\Sample'
field = "Date_Field"

with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])

Id. de artículo:000023088

Recibir notificaciones y encontrar soluciones a problemas nuevos o comunes

Obtenga respuestas resumidas y soluciones de vídeo de nuestro nuevo chatbot de IA.

Descargar la aplicación de soporte de Esri

Información relacionada

Descubrir más sobre este tema

Obtener ayuda de expertos en ArcGIS

Contactar con el soporte técnico

Descargar la aplicación de soporte de Esri

Ir a opciones de descarga