[GIS] Pulling week number from separate date column using Python and ArcPy for ArcGIS Pro

arcgis-proarcpydatetime

I have a date field that contains date/time in the format DD:MM:YYYY HH:MM:SS. I want to be able to calculate the week number based on these dates in a seperate column using Python in ArcGIS Pro. I am able to get the week number based on the current date using the following code:

datetime.date.today().strftime("%W")

Unfortunately I can't translate this into pulling the week number from a separate column, rather than the current date.

Best Answer

You can use the UpdateCursor to read the date field and update the week field:

UpdateCursor establishes read-write access to records returned from a feature class or table.

The code can be executed in the Python window. You need to change the name of the feature class and fields to match your data:

import arcpy,datetime
with arcpy.da.UpdateCursor("Areas_date",['Date','Week']) as cursor:
    for row in cursor:
        row[1]=row[0].strftime("%W")
        cursor.updateRow(row)

Screenshot from ArcMap but it is the same in Pro: enter image description here

Related Question