操作方法

操作方法:通过 Python 使用当前日期来填充日期字段

Last Published: July 20, 2023

摘要

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.

过程

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:000023088

接收通知并查找新问题或常见问题的解决方案

从我们全新的 AI 聊天机器人中获得简明答案和视频解决方案。

下载 Esri 支持应用程序

相关信息

发现关于本主题的更多内容

获取来自 ArcGIS 专家的帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项